Skip to content

Commit

Permalink
🐛 Remove requirement to specify CAPIProvider secret (#788)
Browse files Browse the repository at this point in the history
* Remove requirement to specify CAPIProvider secret

Signed-off-by: Danil-Grigorev <[email protected]>

* Update tests to check configSecret field propagation

Signed-off-by: Danil-Grigorev <[email protected]>

* Use previous CAPIProvider configuration for chart upgrade e2e

Signed-off-by: Danil-Grigorev <[email protected]>

* Combine CAPIProvider variables witin secret with defaults

Signed-off-by: Danil-Grigorev <[email protected]>

---------

Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev authored Oct 22, 2024
1 parent 863fdb6 commit f4d8547
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 33 deletions.
6 changes: 6 additions & 0 deletions internal/sync/azure_provider_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func NewAzureProviderSync(cl client.Client, capiProvider *turtlesv1.CAPIProvider

capiProvider.SetSpec(spec)

if capiProvider.Spec.Variables == nil {
capiProvider.Spec.Variables = map[string]string{}
}

capiProvider.Spec.Variables["EXP_AKS_RESOURCE_HEALTH"] = "true"

return &ProviderSync{
DefaultSynchronizer: NewDefaultSynchronizer(cl, capiProvider, template),
Destination: destination,
Expand Down
10 changes: 9 additions & 1 deletion internal/sync/secret_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ limitations under the License.
package sync

import (
"cmp"
"context"
"encoding/base64"
"maps"
"strconv"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"

turtlesv1 "github.com/rancher/turtles/api/v1alpha1"
)

Expand Down Expand Up @@ -71,6 +75,10 @@ func (SecretSync) Template(capiProvider *turtlesv1.CAPIProvider) client.Object {
func (s *SecretSync) Sync(_ context.Context) error {
s.SyncObjects()

s.Source.Spec.ProviderSpec.ConfigSecret = cmp.Or(s.Source.Spec.ProviderSpec.ConfigSecret, &operatorv1.SecretReference{
Name: s.Source.Name,
})

return nil
}

Expand Down Expand Up @@ -99,7 +107,7 @@ func (s *SecretSync) SyncObjects() {

func setVariables(capiProvider *turtlesv1.CAPIProvider) {
if capiProvider.Spec.Variables != nil {
capiProvider.Status.Variables = capiProvider.Spec.Variables
maps.Copy(capiProvider.Status.Variables, capiProvider.Spec.Variables)
}
}

Expand Down
41 changes: 37 additions & 4 deletions internal/sync/secret_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
turtlesv1 "github.com/rancher/turtles/api/v1alpha1"
"github.com/rancher/turtles/internal/sync"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -30,10 +31,11 @@ import (

var _ = Describe("Provider sync", func() {
var (
err error
ns *corev1.Namespace
capiProvider *turtlesv1.CAPIProvider
secret *corev1.Secret
err error
ns *corev1.Namespace
capiProvider *turtlesv1.CAPIProvider
infrastructure *operatorv1.InfrastructureProvider
secret *corev1.Secret
)

BeforeEach(func() {
Expand Down Expand Up @@ -67,6 +69,11 @@ var _ = Describe("Provider sync", func() {
Namespace: capiProvider.Namespace,
}}

infrastructure = &operatorv1.InfrastructureProvider{ObjectMeta: metav1.ObjectMeta{
Name: string(capiProvider.Spec.Name),
Namespace: ns.Name,
}}

Expect(testEnv.Client.Create(ctx, capiProvider)).To(Succeed())
})

Expand All @@ -86,7 +93,33 @@ var _ = Describe("Provider sync", func() {

Eventually(Object(secret)).Should(
HaveField("Data", HaveKey("variable")))

// Defaults expectations
Eventually(Object(secret)).Should(
HaveField("Data", HaveKey("EXP_MACHINE_POOL")))
Eventually(Object(secret)).Should(
HaveField("Data", HaveKey("CLUSTER_TOPOLOGY")))
Eventually(Object(secret)).Should(
HaveField("Data", HaveKey("EXP_CLUSTER_RESOURCE_SET")))
})

It("Should sync the configSecret default", func() {
capiProvider := capiProvider.DeepCopy()

s := sync.NewList(
sync.NewSecretSync(testEnv, capiProvider),
sync.NewProviderSync(testEnv, capiProvider),
)

Eventually(func(g Gomega) {
g.Expect(s.Sync(ctx)).To(Succeed())

err = nil
s.Apply(ctx, &err)
g.Expect(err).To(Succeed())

g.Expect(testEnv.Get(ctx, client.ObjectKeyFromObject(infrastructure), infrastructure)).ToNot(HaveOccurred())
g.Expect(infrastructure.Spec.ConfigSecret).ToNot(BeNil())
}).Should(Succeed())
})
})
3 changes: 3 additions & 0 deletions test/e2e/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var (
//go:embed data/capi-operator/capi-providers.yaml
CapiProviders []byte

//go:embed data/capi-operator/capi-providers-legacy.yaml
CapiProvidersLegacy []byte

//go:embed data/capi-operator/capv-provider.yaml
CapvProvider []byte

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/data/capi-operator/capa-variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
apiVersion: v1
kind: Secret
metadata:
name: full-variables
name: aws
namespace: capa-system
type: Opaque
stringData:
Expand Down
48 changes: 48 additions & 0 deletions test/e2e/data/capi-operator/capi-providers-legacy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: capd-system
---
apiVersion: turtles-capi.cattle.io/v1alpha1
kind: CAPIProvider
metadata:
name: docker
namespace: capd-system
spec:
name: docker
type: infrastructure
configSecret:
name: variables
---
apiVersion: v1
kind: Namespace
metadata:
name: capi-kubeadm-bootstrap-system
---
apiVersion: turtles-capi.cattle.io/v1alpha1
kind: CAPIProvider
metadata:
name: kubeadm-bootstrap
namespace: capi-kubeadm-bootstrap-system
spec:
name: kubeadm
type: bootstrap
configSecret:
name: variables
---
apiVersion: v1
kind: Namespace
metadata:
name: capi-kubeadm-control-plane-system
---
apiVersion: turtles-capi.cattle.io/v1alpha1
kind: CAPIProvider
metadata:
name: kubeadm-control-plane
namespace: capi-kubeadm-control-plane-system
spec:
name: kubeadm
type: controlPlane
configSecret:
name: variables
7 changes: 0 additions & 7 deletions test/e2e/data/capi-operator/capi-providers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ metadata:
name: docker
namespace: capd-system
spec:
name: docker
type: infrastructure
configSecret:
name: variables
---
apiVersion: v1
kind: Namespace
Expand All @@ -28,8 +25,6 @@ metadata:
spec:
name: kubeadm
type: bootstrap
configSecret:
name: variables
---
apiVersion: v1
kind: Namespace
Expand All @@ -44,5 +39,3 @@ metadata:
spec:
name: kubeadm
type: controlPlane
configSecret:
name: variables
9 changes: 1 addition & 8 deletions test/e2e/data/capi-operator/capv-provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,4 @@ metadata:
name: vsphere
namespace: capv-system
spec:
name: vsphere
type: infrastructure
configSecret:
name: vsphere-variables
variables:
CLUSTER_TOPOLOGY: "true"
EXP_CLUSTER_RESOURCE_SET: "true"
EXP_MACHINE_POOL: "true"
type: infrastructure
11 changes: 0 additions & 11 deletions test/e2e/data/capi-operator/full-providers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ metadata:
namespace: capa-system
spec:
type: infrastructure
name: aws
configSecret:
name: full-variables
variables:
EXP_MACHINE_POOL: "true"
EXP_EXTERNAL_RESOURCE_GC: "true"
Expand All @@ -23,11 +20,3 @@ metadata:
namespace: capz-system
spec:
type: infrastructure
name: azure
configSecret:
name: azure-variables
variables:
CLUSTER_TOPOLOGY: "true"
EXP_CLUSTER_RESOURCE_SET: "true"
EXP_MACHINE_POOL: "true"
EXP_AKS_RESOURCE_HEALTH: "true"
2 changes: 1 addition & 1 deletion test/e2e/suites/chart-upgrade/chart_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = Describe("Chart upgrade functionality should work", Label(e2e.ShortTestL
BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy,
HelmBinaryPath: e2eConfig.GetVariable(e2e.HelmBinaryPathVar),
TurtlesChartPath: "https://rancher.github.io/turtles",
CAPIProvidersYAML: e2e.CapiProviders,
CAPIProvidersYAML: e2e.CapiProvidersLegacy,
Namespace: framework.DefaultRancherTurtlesNamespace,
Version: "v0.6.0",
WaitDeploymentsReadyInterval: e2eConfig.GetIntervals(setupClusterResult.BootstrapClusterProxy.GetName(), "wait-controllers"),
Expand Down

0 comments on commit f4d8547

Please sign in to comment.