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

refactor: use config version to track resource updates #726

Merged
merged 1 commit into from
Oct 31, 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
3 changes: 3 additions & 0 deletions api/config/crd/bases/odigos.io_odigosconfigurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ spec:
properties:
autoscalerImage:
type: string
configVersion:
type: integer
ignoredNamespaces:
items:
type: string
Expand All @@ -56,6 +58,7 @@ spec:
telemetryEnabled:
type: boolean
required:
- configVersion
- odigosVersion
type: object
type: object
Expand Down
1 change: 1 addition & 0 deletions api/odigos/v1alpha1/odigosconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// OdigosConfigurationSpec defines the desired state of OdigosConfiguration
type OdigosConfigurationSpec struct {
OdigosVersion string `json:"odigosVersion"`
ConfigVersion int `json:"configVersion"`
TelemetryEnabled bool `json:"telemetryEnabled,omitempty"`
SidecarInstrumentation bool `json:"sidecarInstrumentation,omitempty"`
IgnoredNamespaces []string `json:"ignoredNamespaces,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func createCRDs(ctx context.Context, cmd *cobra.Command, client *kube.Client, ns
func createOdigosConfigSpec() odigosv1.OdigosConfigurationSpec {
return odigosv1.OdigosConfigurationSpec{
OdigosVersion: versionFlag,
ConfigVersion: 1, // config version starts at 1 and incremented on every config change
TelemetryEnabled: telemetryEnabled,
SidecarInstrumentation: sidecarInstrumentation,
IgnoredNamespaces: ignoredNamespaces,
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/resources/applyresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func DeleteOldOdigosSystemObjects(ctx context.Context, client *kube.Client, ns s
resources := kube.GetManagedResources(ns)
for _, resource := range resources {
l := log.Print(fmt.Sprintf("Syncing %s", resource.Resource.Resource))
err := client.DeleteOldOdigosSystemObjects(ctx, resource, config.Spec.OdigosVersion)
err := client.DeleteOldOdigosSystemObjects(ctx, resource, config.Spec.ConfigVersion)
if err != nil {
l.Error(err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/resources/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,5 +487,5 @@ func (a *autoScalerResourceManager) InstallFromScratch(ctx context.Context) erro
NewAutoscalerLeaderElectionRoleBinding(a.ns),
NewAutoscalerDeployment(a.ns, a.config.OdigosVersion, a.config.ImagePrefix, a.config.AutoscalerImage),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
5 changes: 4 additions & 1 deletion cli/cmd/resources/crds/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ func NewConfiguration() apiextensionsv1.CustomResourceDefinition {
"spec": {
Description: "OdigosConfigurationSpec defines the desired state of OdigosConfiguration",
Type: "object",
Required: []string{"odigosVersion"},
Required: []string{"odigosVersion", "configVersion"},
Properties: map[string]apiextensionsv1.JSONSchemaProps{
"autoscalerImage": {
Type: "string",
},
"configVersion": {
Type: "integer",
},
"ignoredNamespaces": {
Type: "array",
Items: &apiextensionsv1.JSONSchemaPropsOrArray{
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/resources/datacollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ func (a *dataCollectionResourceManager) InstallFromScratch(ctx context.Context)
NewDataCollectionClusterRole(a.config.Psp),
NewDataCollectionClusterRoleBinding(a.ns),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
2 changes: 1 addition & 1 deletion cli/cmd/resources/instrumentor.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,5 @@ func (a *instrumentorResourceManager) InstallFromScratch(ctx context.Context) er
NewInstrumentorClusterRoleBinding(a.ns),
NewInstrumentorDeployment(a.ns, a.config.OdigosVersion, a.config.TelemetryEnabled, a.config.SidecarInstrumentation, a.config.IgnoredNamespaces, a.config.ImagePrefix, a.config.InstrumentorImage),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
2 changes: 1 addition & 1 deletion cli/cmd/resources/keyvalproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,5 @@ func (a *keyvalProxyResourceManager) InstallFromScratch(ctx context.Context) err
NewKeyvalProxyClusterRoleBinding(a.ns),
NewKeyvalProxyDeployment(odigosCloudProxyVersion, a.ns, a.config.ImagePrefix),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
7 changes: 4 additions & 3 deletions cli/cmd/resources/managers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ func CreateResourceManagers(client *kube.Client, odigosNs string, isOdigosCloud

// Note - the order of resource managers is important.
// If resource A depends on resource B, then A must be installed after B.
resourceManagers := []ResourceManager{}
resourceManagers := []ResourceManager{
NewOdigosDeploymentResourceManager(client, odigosNs, config),
NewOdigosConfigResourceManager(client, odigosNs, config),
}

if isOdigosCloud {
resourceManagers = append(resourceManagers, NewOdigosCloudResourceManager(client, odigosNs, config, apiKey))
}

resourceManagers = append(resourceManagers, []ResourceManager{
NewOdigosDeploymentResourceManager(client, odigosNs, config),
NewOdigosConfigResourceManager(client, odigosNs, config),
NewOwnTelemetryResourceManager(client, odigosNs, config, isOdigosCloud),
NewDataCollectionResourceManager(client, odigosNs, config),
NewInstrumentorResourceManager(client, odigosNs, config),
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/resources/odiglet.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,5 @@ func (a *odigletResourceManager) InstallFromScratch(ctx context.Context) error {
NewOdigletClusterRoleBinding(a.ns),
NewOdigletDaemonSet(a.ns, a.config.OdigosVersion, a.config.ImagePrefix, a.config.OdigletImage),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
4 changes: 2 additions & 2 deletions cli/cmd/resources/odigoscloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func (a *odigosCloudResourceManager) InstallFromScratch(ctx context.Context) err
// ERROR metadata.managedFields must be nil
// But not sure if this is the right way to fix it.
sec.ManagedFields = nil
return a.client.ApplyResources(ctx, a.config.OdigosVersion, []client.Object{sec})
return a.client.ApplyResources(ctx, a.config.ConfigVersion, []client.Object{sec})
}

resources := []client.Object{
NewKeyvalSecret(a.ns, *a.apiKey),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
2 changes: 1 addition & 1 deletion cli/cmd/resources/odigosconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func (a *odigosConfigResourceManager) InstallFromScratch(ctx context.Context) er
resources := []client.Object{
NewOdigosConfiguration(a.ns, a.config),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
2 changes: 1 addition & 1 deletion cli/cmd/resources/odigosdeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ func (a *odigosDeploymentResourceManager) InstallFromScratch(ctx context.Context
NewOdigosDeploymentConfigMap(a.ns, a.config.OdigosVersion),
NewLeaderElectionRole(a.ns),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
2 changes: 1 addition & 1 deletion cli/cmd/resources/owntelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,5 @@ func (a *ownTelemetryResourceManager) InstallFromScratch(ctx context.Context) er
NewOwnTelemetryConfigMapDisabled(a.ns),
}
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
2 changes: 1 addition & 1 deletion cli/cmd/resources/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,5 @@ func (a *schedulerResourceManager) InstallFromScratch(ctx context.Context) error
NewSchedulerClusterRoleBinding(a.ns),
NewSchedulerDeployment(a.ns, a.config.OdigosVersion, a.config.ImagePrefix),
}
return a.client.ApplyResources(ctx, a.config.OdigosVersion, resources)
return a.client.ApplyResources(ctx, a.config.ConfigVersion, resources)
}
3 changes: 3 additions & 0 deletions cli/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ and apply any required migrations and adaptations.`,
fmt.Println("Odigos upgrade failed - unable to read the current Odigos configuration.")
os.Exit(1)
}

// update the config on upgrade
config.Spec.OdigosVersion = versionFlag
config.Spec.ConfigVersion += 1

isOdigosCloud, err := resources.IsOdigosCloud(ctx, client, ns)
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions cli/pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strconv"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -69,24 +70,24 @@ func PrintClientErrorAndExit(err error) {
os.Exit(-1)
}

func (c *Client) ApplyResources(ctx context.Context, odigosVersion string, objs []client.Object) error {
func (c *Client) ApplyResources(ctx context.Context, configVersion int, objs []client.Object) error {
for _, obj := range objs {
err := c.ApplyResource(ctx, odigosVersion, obj)
err := c.ApplyResource(ctx, configVersion, obj)
if err != nil {
return err
}
}
return nil
}

func (c *Client) ApplyResource(ctx context.Context, odigosVersion string, obj client.Object) error {
func (c *Client) ApplyResource(ctx context.Context, configVersion int, obj client.Object) error {

labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[odigoslabels.OdigosSystemVersionLabelKey] = odigosVersion
labels[odigoslabels.OdigosSystemLabelKey] = odigoslabels.OdigosSystemLabelValue
labels[odigoslabels.OdigosSystemConfigLabelKey] = strconv.Itoa(configVersion)
obj.SetLabels(labels)

depBytes, _ := yaml.Marshal(obj)
Expand All @@ -105,9 +106,9 @@ func (c *Client) ApplyResource(ctx context.Context, odigosVersion string, obj cl
return err
}

func (c *Client) DeleteOldOdigosSystemObjects(ctx context.Context, resourceAndNamespace ResourceAndNs, odigosVersion string) error {
func (c *Client) DeleteOldOdigosSystemObjects(ctx context.Context, resourceAndNamespace ResourceAndNs, configVersion int) error {
systemObject, _ := k8slabels.NewRequirement(odigoslabels.OdigosSystemLabelKey, selection.Equals, []string{odigoslabels.OdigosSystemLabelValue})
notLatestVersion, _ := k8slabels.NewRequirement(odigoslabels.OdigosSystemVersionLabelKey, selection.NotEquals, []string{odigosVersion})
notLatestVersion, _ := k8slabels.NewRequirement(odigoslabels.OdigosSystemConfigLabelKey, selection.NotEquals, []string{strconv.Itoa(configVersion)})
labelSelector := k8slabels.NewSelector().Add(*systemObject).Add(*notLatestVersion).String()
resource := resourceAndNamespace.Resource
ns := resourceAndNamespace.Namespace
Expand Down
6 changes: 3 additions & 3 deletions cli/pkg/labels/labels.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package labels

const (
OdigosSystemLabelKey = "odigos.io/system-object"
OdigosSystemVersionLabelKey = "odigos.io/version"
OdigosSystemLabelValue = "true"
OdigosSystemLabelKey = "odigos.io/system-object"
OdigosSystemConfigLabelKey = "odigos.io/config"
OdigosSystemLabelValue = "true"
)

var OdigosSystem = map[string]string{
Expand Down