Skip to content

Commit 52cfe87

Browse files
committed
Rebase on CAPI v1.6.0
1 parent 76a3ba7 commit 52cfe87

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ CONTROLLER_GEN_VER := v0.11.4
7070
CONTROLLER_GEN_BIN := controller-gen
7171
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
7272

73-
GOLANGCI_LINT_VER := v1.53.2
73+
GOLANGCI_LINT_VER := v1.55.2
7474
GOLANGCI_LINT_BIN := golangci-lint
7575
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)
7676

cmd/plugin/cmd/init.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ type initOptions struct {
6363
}
6464

6565
const (
66-
capiOperatorProviderName = "capi-operator"
67-
capiOperatorManifestsURL = "https://github.com/kubernetes-sigs/cluster-api-operator/releases/latest/operator-components.yaml"
66+
capiOperatorProviderName = "capi-operator"
67+
capiOperatorManifestsURLTemplate = "https://github.com/kubernetes-sigs/cluster-api-operator/releases/%s/operator-components.yaml"
6868
)
6969

7070
var initOpts = &initOptions{}
@@ -127,7 +127,7 @@ func init() {
127127
"Path to the kubeconfig for the management cluster. If unspecified, default discovery rules apply.")
128128
initCmd.PersistentFlags().StringVar(&initOpts.kubeconfigContext, "kubeconfig-context", "",
129129
"Context to be used within the kubeconfig file. If empty, current context will be used.")
130-
initCmd.PersistentFlags().StringVar(&initOpts.operatorVersion, "operator-version", "",
130+
initCmd.PersistentFlags().StringVar(&initOpts.operatorVersion, "operator-version", GetLatestPluginVersion(),
131131
"CAPI Operator version (e.g. v0.7.0) to install on the management cluster. If unspecified, the latest release is used.")
132132
initCmd.PersistentFlags().StringVar(&initOpts.coreProvider, "core", "",
133133
"Core provider version (e.g. cluster-api:v1.1.5) to add to the management cluster. If unspecified, Cluster API's latest release is used.")
@@ -168,7 +168,7 @@ func runInit() error {
168168
log.Info("Checking that Cert Manager is installed and running.")
169169

170170
// Ensure that cert manager is installed.
171-
if err := ensureCertManager(initOpts); err != nil {
171+
if err := ensureCertManager(ctx, initOpts); err != nil {
172172
return fmt.Errorf("cannot ensure that cert manager is installed: %w", err)
173173
}
174174

@@ -187,7 +187,7 @@ func runInit() error {
187187
if !deploymentExists {
188188
log.Info("Installing CAPI operator", "Version", initOpts.operatorVersion)
189189

190-
if err := deployCAPIOperator(initOpts); err != nil {
190+
if err := deployCAPIOperator(ctx, initOpts); err != nil {
191191
return fmt.Errorf("cannot deploy CAPI operator: %w", err)
192192
}
193193

@@ -340,8 +340,8 @@ func checkProviderReadiness(ctx context.Context, client ctrlclient.Client, gener
340340
}
341341
}
342342

343-
func ensureCertManager(opts *initOptions) error {
344-
configClient, err := configclient.New("")
343+
func ensureCertManager(ctx context.Context, opts *initOptions) error {
344+
configClient, err := configclient.New(ctx, "")
345345
if err != nil {
346346
return fmt.Errorf("cannot create config client: %w", err)
347347
}
@@ -355,23 +355,29 @@ func ensureCertManager(opts *initOptions) error {
355355

356356
// Before installing the operator, ensure the cert-manager Webhook is in place.
357357
certManager := clusterClient.CertManager()
358-
if err := certManager.EnsureInstalled(); err != nil {
358+
if err := certManager.EnsureInstalled(ctx); err != nil {
359359
return fmt.Errorf("cannot install cert-manager Webhook: %w", err)
360360
}
361361

362362
return nil
363363
}
364364

365365
// deployCAPIOperator deploys the CAPI operator on the management cluster.
366-
func deployCAPIOperator(opts *initOptions) error {
367-
configClient, err := configclient.New("")
366+
func deployCAPIOperator(ctx context.Context, opts *initOptions) error {
367+
configClient, err := configclient.New(ctx, "")
368368
if err != nil {
369369
return fmt.Errorf("cannot create config client: %w", err)
370370
}
371371

372+
if opts.operatorVersion == "" {
373+
return fmt.Errorf("cannot deploy CAPI operator without specifying an operator version")
374+
}
375+
376+
capiOperatorManifestsURL := fmt.Sprintf(capiOperatorManifestsURLTemplate, opts.operatorVersion)
377+
372378
providerConfig := configclient.NewProvider(capiOperatorProviderName, capiOperatorManifestsURL, clusterctlv1.ProviderTypeUnknown)
373379

374-
repo, err := util.RepositoryFactory(providerConfig, configClient.Variables())
380+
repo, err := util.RepositoryFactory(ctx, providerConfig, configClient.Variables())
375381
if err != nil {
376382
return fmt.Errorf("cannot create repository: %w", err)
377383
}
@@ -380,7 +386,7 @@ func deployCAPIOperator(opts *initOptions) error {
380386
opts.operatorVersion = repo.DefaultVersion()
381387
}
382388

383-
componentsFile, err := repo.GetFile(opts.operatorVersion, repo.ComponentsPath())
389+
componentsFile, err := repo.GetFile(ctx, opts.operatorVersion, repo.ComponentsPath())
384390
if err != nil {
385391
return fmt.Errorf("cannot get components file: %w", err)
386392
}
@@ -409,10 +415,7 @@ func deployCAPIOperator(opts *initOptions) error {
409415

410416
clusterClient := cluster.New(clusterKubeconfig, configClient)
411417

412-
// TODO(mfedosin): we have to ignore wait-providers and wait-providers-timeout options until we upgrade
413-
// to CAPI v1.6.0 where clusterctl functions have Context support.
414-
415-
if err := clusterClient.ProviderComponents().Create(components.Objs()); err != nil {
418+
if err := clusterClient.ProviderComponents().Create(ctx, components.Objs()); err != nil {
416419
return fmt.Errorf("cannot create CAPI operator components: %w", err)
417420
}
418421

cmd/plugin/cmd/init_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,22 +405,22 @@ func TestDeployCAPIOperator(t *testing.T) {
405405
wantErr bool
406406
}{
407407
{
408-
name: "no version",
409-
wantedVersion: "",
408+
name: "with version",
409+
wantedVersion: "v0.7.0",
410410
wantErr: false,
411411
opts: &initOptions{
412412
kubeconfig: kubeconfigFile.Name(),
413413
kubeconfigContext: "@test-cluster",
414+
operatorVersion: "v0.7.0",
414415
},
415416
},
416417
{
417-
name: "old version",
418-
wantedVersion: "v0.6.0",
419-
wantErr: false,
418+
name: "without version",
419+
wantErr: true,
420420
opts: &initOptions{
421421
kubeconfig: kubeconfigFile.Name(),
422422
kubeconfigContext: "@test-cluster",
423-
operatorVersion: "v0.6.0",
423+
operatorVersion: "",
424424
},
425425
},
426426
{
@@ -429,19 +429,23 @@ func TestDeployCAPIOperator(t *testing.T) {
429429
opts: &initOptions{
430430
kubeconfig: kubeconfigFile.Name(),
431431
kubeconfigContext: "@test-cluster",
432-
operatorVersion: "v666.6.6",
432+
operatorVersion: "v1000000",
433433
},
434434
},
435435
}
436436
for _, tt := range tests {
437437
t.Run(tt.name, func(t *testing.T) {
438438
g := NewWithT(t)
439439

440+
ctx, cancel := context.WithTimeout(context.Background(), waitShort)
441+
442+
defer cancel()
443+
440444
resources := []ctrlclient.Object{}
441445

442446
deployment := generateCAPIOperatorDeployment("capi-operator-controller-manager", "capi-operator-system")
443447

444-
err := deployCAPIOperator(tt.opts)
448+
err := deployCAPIOperator(ctx, tt.opts)
445449

446450
if tt.wantErr {
447451
g.Expect(err).To(HaveOccurred())

cmd/plugin/cmd/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cmd
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223

2324
corev1 "k8s.io/api/core/v1"
2425
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -30,6 +31,7 @@ import (
3031
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3132

3233
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
34+
"sigs.k8s.io/cluster-api-operator/version"
3335
)
3436

3537
// CreateKubeClient creates a kubernetes client from provided kubeconfig and kubecontext.
@@ -82,3 +84,15 @@ func EnsureNamespaceExists(ctx context.Context, client ctrlclient.Client, namesp
8284

8385
return nil
8486
}
87+
88+
// GetLatestPluginVersion returns the latest released plugin version.
89+
// If git version is v0.7.0-44-b68cd3410df54f-dirty, the function returns v0.7.0
90+
func GetLatestPluginVersion() string {
91+
gitVersion := version.Get().GitVersion
92+
93+
if gitVersion == "" {
94+
return ""
95+
}
96+
97+
return strings.Split(gitVersion, "-")[0]
98+
}

0 commit comments

Comments
 (0)