Skip to content

Commit

Permalink
chore: attempt to fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-ibra committed Jul 30, 2024
1 parent b9ece9a commit fdabf73
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 64 deletions.
12 changes: 9 additions & 3 deletions api/v1alpha1/validatorconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ type Sink struct {

// HelmRelease defines the configuration for a Helm chart release.
type HelmRelease struct {
// Chart defines the Helm chart to be installed.
Chart HelmChart `json:"chart" yaml:"chart"`

// Values defines the values to be passed to the Helm chart.
Values string `json:"values" yaml:"values"`
}

// HelmChart defines the configuration for a Helm chart.
type HelmChart struct {
// Name of the Helm chart.
Name string `json:"name" yaml:"name"`

Expand All @@ -53,9 +62,6 @@ type HelmRelease struct {

// Version of the Helm chart.
Version string `json:"version" yaml:"version"`

// Values defines the values to be passed to the Helm chart.
Values string `json:"values" yaml:"values"`
}

// HelmConfig defines the configuration for the Helm registry.
Expand Down
16 changes: 16 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chart/validator/README.md

Large diffs are not rendered by default.

49 changes: 28 additions & 21 deletions chart/validator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ helmConfig:

# Validation plugin charts
plugins:
- name: validator-plugin-azure
repository: validator-plugin-azure
version: v0.0.14
- chart:
name: validator-plugin-azure
repository: validator-plugin-azure
version: v0.0.14
values: |-
controllerManager:
kubeRbacProxy:
Expand Down Expand Up @@ -264,9 +265,10 @@ plugins:
# Override the service account used by Azure validator (optional, could be used for WorkloadIdentityCredentials on AKS)
# WARNING: the chosen service account must include all RBAC privileges found in templates/manager-rbac.yaml
serviceAccountName: ""
- name: validator-plugin-oci
repository: validator-plugin-oci
version: v0.0.12
- chart:
name: validator-plugin-oci
repository: validator-plugin-oci
version: v0.0.12
values: |-
controllerManager:
kubeRbacProxy:
Expand Down Expand Up @@ -320,9 +322,10 @@ plugins:
protocol: TCP
targetPort: https
type: ClusterIP
- name: validator-plugin-kubescape
repository: validator-plugin-kubescape
version: v0.0.4
- chart:
name: validator-plugin-kubescape
repository: validator-plugin-kubescape
version: v0.0.4
values: |-
controllerManager:
kubeRbacProxy:
Expand Down Expand Up @@ -384,9 +387,10 @@ plugins:
protocol: TCP
targetPort: https
type: ClusterIP
- name: validator-plugin-aws
repository: validator-plugin-aws
version: v0.1.2
- chart:
name: validator-plugin-aws
repository: validator-plugin-aws
version: v0.1.2
values: |-
controllerManager:
kubeRbacProxy:
Expand Down Expand Up @@ -450,9 +454,10 @@ plugins:
# Override the service account used by AWS validator (optional, could be used for IAM roles for Service Accounts)
# WARNING: the chosen service account must have the same RBAC privileges as seen in templates/manager-rbac.yaml
serviceAccountName: ""
- name: validator-plugin-network
repository: validator-plugin-network
version: v0.0.20
- chart:
name: validator-plugin-network
repository: validator-plugin-network
version: v0.0.20
values: |-
controllerManager:
kubeRbacProxy:
Expand Down Expand Up @@ -508,9 +513,10 @@ plugins:
protocol: TCP
targetPort: https
type: ClusterIP
- name: validator-plugin-maas
repository: validator-plugin-maas
version: v0.0.5
- chart:
name: validator-plugin-maas
repository: validator-plugin-maas
version: v0.0.5
values: |-
controllerManager:
kubeRbacProxy:
Expand Down Expand Up @@ -564,9 +570,10 @@ plugins:
protocol: TCP
targetPort: https
type: ClusterIP
- name: validator-plugin-vsphere
repository: validator-plugin-vsphere
version: v0.0.28
- chart:
name: validator-plugin-vsphere
repository: validator-plugin-vsphere
version: v0.0.28
values: |-
controllerManager:
kubeRbacProxy:
Expand Down
30 changes: 18 additions & 12 deletions config/crd/bases/validation.spectrocloud.labs_validatorconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,30 @@ spec:
description: HelmRelease defines the configuration for a Helm chart
release.
properties:
name:
description: Name of the Helm chart.
type: string
repository:
description: Repository of the Helm chart.
type: string
chart:
description: Chart defines the Helm chart to be installed.
properties:
name:
description: Name of the Helm chart.
type: string
repository:
description: Repository of the Helm chart.
type: string
version:
description: Version of the Helm chart.
type: string
required:
- name
- repository
- version
type: object
values:
description: Values defines the values to be passed to the Helm
chart.
type: string
version:
description: Version of the Helm chart.
type: string
required:
- name
- repository
- chart
- values
- version
type: object
type: array
sink:
Expand Down
7 changes: 4 additions & 3 deletions hack/update-versions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ function addChartValues {

# Append the plugin's values to chart/validator/values.yaml
cat <<EOF >> chart/validator/values.yaml
- name: $1
repository: $1
version: v$2
- chart:
name: $1
repository: $1
version: v$2
values: |-
$indentedValues
EOF
Expand Down
7 changes: 4 additions & 3 deletions internal/controller/testdata/vc-network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ spec:
registry: https://validator-labs.github.io
authSecretName: validator-plugin-network-chart-secret
plugins:
- name: validator-plugin-network
repository: validator-plugin-network
version: v0.0.15
- chart:
name: validator-plugin-network
repository: validator-plugin-network
version: v0.0.15
values: |-
controllerManager:
kubeRbacProxy:
Expand Down
36 changes: 18 additions & 18 deletions internal/controller/validatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,23 @@ func (r *ValidatorConfigReconciler) redeployIfNeeded(ctx context.Context, vc *v1

helmConfig := vc.Spec.HelmConfig
for i, p := range vc.Spec.Plugins {
specPlugins[p.Name] = true
specPlugins[p.Chart.Name] = true

// update plugin's values hash
valuesUnchanged := r.updatePluginHash(vc, p)

// skip plugin if already deployed & no change in values
condition, ok := isConditionTrue(vc, p.Name, v1alpha1.HelmChartDeployedCondition)
condition, ok := isConditionTrue(vc, p.Chart.Name, v1alpha1.HelmChartDeployedCondition)
if ok && valuesUnchanged {
r.Log.V(0).Info("Values unchanged. Skipping upgrade for plugin Helm chart", "namespace", vc.Namespace, "name", p.Name)
r.Log.V(0).Info("Values unchanged. Skipping upgrade for plugin Helm chart", "namespace", vc.Namespace, "name", p.Chart.Name)
conditions[i] = condition
continue
}

opts := &helm.Options{
Chart: p.Name,
Repo: fmt.Sprintf("%s/%s", helmConfig.Registry, p.Repository),
Version: p.Version,
Chart: p.Chart.Name,
Repo: fmt.Sprintf("%s/%s", helmConfig.Registry, p.Chart.Repository),
Version: p.Chart.Version,
Values: p.Values,
InsecureSkipTLSVerify: helmConfig.InsecureSkipTLSVerify,
}
Expand All @@ -177,14 +177,14 @@ func (r *ValidatorConfigReconciler) redeployIfNeeded(ctx context.Context, vc *v1
nn := types.NamespacedName{Name: helmConfig.AuthSecretName, Namespace: vc.Namespace}
if err := r.configureHelmOpts(ctx, nn, opts); err != nil {
r.Log.V(0).Error(err, "failed to configure basic auth for Helm upgrade")
conditions[i] = r.buildHelmChartCondition(p.Name, err)
conditions[i] = r.buildHelmChartCondition(p.Chart.Name, err)
continue
}
}

var cleanupLocalChart bool
if strings.HasPrefix(helmConfig.Registry, oci.Scheme) {
r.Log.V(0).Info("Pulling plugin Helm chart", "name", p.Name)
r.Log.V(0).Info("Pulling plugin Helm chart", "name", p.Chart.Name)

opts.Path = fmt.Sprintf("/charts/%s", opts.Chart)
opts.Version = strings.TrimPrefix(opts.Version, "v")
Expand All @@ -197,7 +197,7 @@ func (r *ValidatorConfigReconciler) redeployIfNeeded(ctx context.Context, vc *v1
)
if err != nil {
r.Log.V(0).Error(err, "failed to create OCI client")
conditions[i] = r.buildHelmChartCondition(p.Name, err)
conditions[i] = r.buildHelmChartCondition(p.Chart.Name, err)
continue
}
ociOpts := oci.ImageOptions{
Expand All @@ -207,27 +207,27 @@ func (r *ValidatorConfigReconciler) redeployIfNeeded(ctx context.Context, vc *v1
}
if err := ociClient.PullChart(ociOpts); err != nil {
r.Log.V(0).Error(err, "failed to pull Helm chart from OCI registry")
conditions[i] = r.buildHelmChartCondition(p.Name, err)
conditions[i] = r.buildHelmChartCondition(p.Chart.Name, err)
continue
}

r.Log.V(0).Info("Reconfiguring Helm options to deploy local chart", "name", p.Name)
r.Log.V(0).Info("Reconfiguring Helm options to deploy local chart", "name", p.Chart.Name)
opts.Path = fmt.Sprintf("%s/%s.tgz", opts.Path, opts.Chart)
opts.Chart = ""
cleanupLocalChart = true
}

r.Log.V(0).Info("Installing/upgrading plugin Helm chart", "namespace", vc.Namespace, "name", p.Name)
err := r.HelmClient.Upgrade(p.Name, vc.Namespace, *opts)
r.Log.V(0).Info("Installing/upgrading plugin Helm chart", "namespace", vc.Namespace, "name", p.Chart.Name)
err := r.HelmClient.Upgrade(p.Chart.Name, vc.Namespace, *opts)
if err != nil {
// if Helm install/upgrade failed, delete the release so installation is reattempted each iteration
if strings.Contains(err.Error(), "has no deployed releases") {
if err := r.HelmClient.Delete(p.Name, vc.Namespace); err != nil {
if err := r.HelmClient.Delete(p.Chart.Name, vc.Namespace); err != nil {
r.Log.V(0).Error(err, "failed to delete Helm release")
}
}
}
conditions[i] = r.buildHelmChartCondition(p.Name, err)
conditions[i] = r.buildHelmChartCondition(p.Chart.Name, err)

if cleanupLocalChart {
r.Log.V(0).Info("Cleaning up local chart directory", "path", opts.Path)
Expand Down Expand Up @@ -291,7 +291,7 @@ func (r *ValidatorConfigReconciler) updatePluginHash(vc *v1alpha1.ValidatorConfi
valuesUnchanged := false
pluginValuesHashLatest := sha256.Sum256([]byte(p.Values))
pluginValuesHashLatestB64 := base64.StdEncoding.EncodeToString(pluginValuesHashLatest[:])
key := getPluginHashKey(p.Name)
key := getPluginHashKey(p.Chart.Name)

pluginValuesHash, ok := vc.Annotations[key]
if ok {
Expand All @@ -311,7 +311,7 @@ func getPluginHashKey(pluginName string) string {
func (r *ValidatorConfigReconciler) deletePlugins(ctx context.Context, vc *v1alpha1.ValidatorConfig) error {
var wg sync.WaitGroup
for _, p := range vc.Spec.Plugins {
release, err := r.HelmReleaseClient.Get(ctx, p.Name, vc.Namespace)
release, err := r.HelmReleaseClient.Get(ctx, p.Chart.Name, vc.Namespace)
if err != nil {
if !apierrs.IsNotFound(err) {
return err
Expand All @@ -326,7 +326,7 @@ func (r *ValidatorConfigReconciler) deletePlugins(ctx context.Context, vc *v1alp
go func(name string) {
defer wg.Done()
r.deletePlugin(vc, name)
}(p.Name)
}(p.Chart.Name)
}

wg.Wait()
Expand Down
8 changes: 5 additions & 3 deletions internal/controller/validatorconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var _ = Describe("ValidatorConfig controller", Ordered, func() {
if err := k8sClient.Get(ctx, vcKey, vc); err != nil {
return false
}
vc.Spec.Plugins[0].Version = networkPluginVersionPost
vc.Spec.Plugins[0].Chart.Version = networkPluginVersionPost
vc.Spec.Plugins[0].Values = strings.ReplaceAll(
vc.Spec.Plugins[0].Values, networkPluginVersionPre, networkPluginVersionPost,
)
Expand Down Expand Up @@ -153,8 +153,10 @@ var _ = Describe("ValidatorConfig controller", Ordered, func() {
},
Plugins: []v1alpha1.HelmRelease{
{
Repository: "bar",
Name: "bar",
Chart: v1alpha1.HelmChart{
Repository: "bar",
Name: "bar",
},
},
},
},
Expand Down

0 comments on commit fdabf73

Please sign in to comment.