Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 new logic to customise inventory config clone #1880

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions providers-sdk/v1/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,40 @@ func (p *Platform) PrettyTitle() string {
return prettyTitle
}

func (cfg *Config) Clone() *Config {
type cloneSettings struct {
noDiscovery bool
}

type CloneOption interface {
Apply(*cloneSettings)
}

// WithoutDiscovery removes the discovery flags in the opts to ensure the same discovery does not run again
func WithoutDiscovery() CloneOption {
return withoutDiscovery{}
}

type withoutDiscovery struct{}

func (w withoutDiscovery) Apply(o *cloneSettings) { o.noDiscovery = true }

func (cfg *Config) Clone(opts ...CloneOption) *Config {
if cfg == nil {
return nil
}
return proto.Clone(cfg).(*Config)

cloneSettings := &cloneSettings{}
for _, option := range opts {
option.Apply(cloneSettings)
}

clonedObject := proto.Clone(cfg).(*Config)

if cloneSettings.noDiscovery {
clonedObject.Discover = &Discovery{}
}

return clonedObject
}

func (c *Config) ToUrl() string {
Expand Down
25 changes: 9 additions & 16 deletions providers/gcp/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
Kind: "gcp-object",
},
Labels: map[string]string{},
Connections: []*inventory.Config{cloneConfig(conn.Conf)},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())},
})
}

Expand Down Expand Up @@ -195,7 +195,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Labels: labels,
// TODO: the current connection handling does not work well for instances
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
Kind: "gcp-object",
},
Labels: labels,
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}
Expand All @@ -254,7 +254,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
Kind: "gcp-object",
},
Labels: map[string]string{},
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
Kind: "gcp-object",
},
Labels: map[string]string{},
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}
Expand All @@ -312,7 +312,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
Kind: "gcp-object",
},
Labels: map[string]string{},
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}
Expand All @@ -339,7 +339,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
Kind: "gcp-object",
},
Labels: map[string]string{},
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}
Expand All @@ -366,7 +366,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
Kind: "gcp-object",
},
Labels: map[string]string{},
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}
Expand All @@ -393,21 +393,14 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
Kind: "gcp-object",
},
Labels: map[string]string{},
Connections: []*inventory.Config{cloneConfig(conn.Conf)}, // pass-in the parent connection config
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
}
}

return assetList, nil
}

func cloneConfig(invConf *inventory.Config) *inventory.Config {
invConfClone := invConf.Clone()
// We do not want to run discovery again for the already discovered assets
invConfClone.Discover = &inventory.Discovery{}
return invConfClone
}

func resolveGcr(ctx context.Context, conf *inventory.Config) ([]*inventory.Asset, error) {
resolved := []*inventory.Asset{}
repository := conf.Host
Expand Down
27 changes: 10 additions & 17 deletions providers/k8s/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
PlatformIds: []string{assetId},
Name: conn.Name(),
Platform: conn.Platform(),
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
}
if slices.Contains(invConfig.Discover.Targets, DiscoveryClusters) {
in.Spec.Assets = append(in.Spec.Assets, root)
Expand Down Expand Up @@ -296,7 +296,7 @@ func discoverPods(
Name: pod.Namespace.Data + "/" + pod.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(pod.obj)
}
Expand Down Expand Up @@ -340,7 +340,7 @@ func discoverJobs(
Name: job.Namespace.Data + "/" + job.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(job.obj)
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func discoverCronJobs(
Name: cjob.Namespace.Data + "/" + cjob.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(cjob.obj)
}
Expand Down Expand Up @@ -428,7 +428,7 @@ func discoverStatefulSets(
Name: statefulset.Namespace.Data + "/" + statefulset.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(statefulset.obj)
}
Expand Down Expand Up @@ -472,7 +472,7 @@ func discoverDeployments(
Name: deployment.Namespace.Data + "/" + deployment.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(deployment.obj)
}
Expand Down Expand Up @@ -516,7 +516,7 @@ func discoverReplicaSets(
Name: replicaset.Namespace.Data + "/" + replicaset.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(replicaset.obj)
}
Expand Down Expand Up @@ -560,7 +560,7 @@ func discoverDaemonSets(
Name: daemonset.Namespace.Data + "/" + daemonset.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(daemonset.obj)
}
Expand Down Expand Up @@ -634,7 +634,7 @@ func discoverIngresses(
Name: ingress.Namespace.Data + "/" + ingress.Name.Data,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
od.Add(ingress.obj)
}
Expand Down Expand Up @@ -686,7 +686,7 @@ func discoverNamespaces(
Name: ns.Name,
Platform: platform,
Labels: labels,
Connections: []*inventory.Config{cloneConfig(invConfig)}, // pass-in the parent connection config
Connections: []*inventory.Config{invConfig.Clone(inventory.WithoutDiscovery())}, // pass-in the parent connection config
})
if od != nil {
od.Add(&ns)
Expand Down Expand Up @@ -871,13 +871,6 @@ func createPlatformData(objectKind, runtime string) (*inventory.Platform, error)
return platformData, nil
}

func cloneConfig(invConf *inventory.Config) *inventory.Config {
invConfClone := invConf.Clone()
// We do not want to run discovery again for the already discovered assets
invConfClone.Discover = &inventory.Discovery{}
return invConfClone
}

func setRelatedAssets(conn shared.Connection, root *inventory.Asset, assets []*inventory.Asset, od *PlatformIdOwnershipIndex) {
// everything is connected to the root asset
root.RelatedAssets = append(root.RelatedAssets, assets...)
Expand Down
Loading