Skip to content

Commit 2297ad4

Browse files
committed
Add logging
1 parent 8aa24ea commit 2297ad4

File tree

4 files changed

+93
-17
lines changed

4 files changed

+93
-17
lines changed

cmd/plugin/cmd/init.go

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ import (
2222
"os"
2323
"path/filepath"
2424
"strings"
25+
"time"
2526

2627
"github.com/spf13/cobra"
2728

2829
appsv1 "k8s.io/api/apps/v1"
30+
corev1 "k8s.io/api/core/v1"
2931
apierrors "k8s.io/apimachinery/pkg/api/errors"
32+
"k8s.io/apimachinery/pkg/util/wait"
3033
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
3134
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
3235
configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
@@ -123,13 +126,13 @@ func init() {
123126
"Context to be used within the kubeconfig file. If empty, current context will be used.")
124127
initCmd.PersistentFlags().StringVar(&initOpts.operatorVersion, "operator-version", "",
125128
"CAPI Operator version (e.g. v0.7.0) to install on the management cluster. If unspecified, the latest release is used.")
126-
initCmd.PersistentFlags().StringVar(&initOpts.coreProvider, "core", "cluster-api",
129+
initCmd.PersistentFlags().StringVar(&initOpts.coreProvider, "core", "",
127130
"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.")
128131
initCmd.PersistentFlags().StringSliceVarP(&initOpts.infrastructureProviders, "infrastructure", "i", []string{},
129132
"Infrastructure providers and versions (e.g. aws:v0.5.0) to add to the management cluster.")
130-
initCmd.PersistentFlags().StringSliceVarP(&initOpts.bootstrapProviders, "bootstrap", "b", []string{"kubeadm"},
133+
initCmd.PersistentFlags().StringSliceVarP(&initOpts.bootstrapProviders, "bootstrap", "b", []string{},
131134
"Bootstrap providers and versions (e.g. kubeadm:v1.1.5) to add to the management cluster. If unspecified, Kubeadm bootstrap provider's latest release is used.")
132-
initCmd.PersistentFlags().StringSliceVarP(&initOpts.controlPlaneProviders, "control-plane", "c", []string{"kubeadm"},
135+
initCmd.PersistentFlags().StringSliceVarP(&initOpts.controlPlaneProviders, "control-plane", "c", []string{},
133136
"Control plane providers and versions (e.g. kubeadm:v1.1.5) to add to the management cluster. If unspecified, the Kubeadm control plane provider's latest release is used.")
134137
// initCmd.PersistentFlags().StringSliceVar(&initOpts.ipamProviders, "ipam", nil,
135138
// "IPAM providers and versions (e.g. infoblox:v0.0.1) to add to the management cluster.")
@@ -157,8 +160,16 @@ func runInit() error {
157160
return fmt.Errorf("cannot create a client: %w", err)
158161
}
159162

160-
// Checking if CAPI operator deployment exists.
161-
deploymentExists, err := checkCAPIOpearatorAvailability(ctx, client)
163+
log.Info("Checking that Cert Manager is installed and running.")
164+
165+
// Ensure that cert manager is installed.
166+
if err := ensureCertManager(initOpts); err != nil {
167+
return fmt.Errorf("cannot ensure that cert manager is installed: %w", err)
168+
}
169+
170+
log.Info("Checking that CAPI Operator is installed and running.")
171+
172+
deploymentExists, err := checkCAPIOperatorAvailability(ctx, client)
162173
if err != nil {
163174
return fmt.Errorf("cannot check CAPI operator availability: %w", err)
164175
}
@@ -167,16 +178,32 @@ func runInit() error {
167178
return fmt.Errorf("cannot specify an operator version when the CAPI operator is already installed")
168179
}
169180

170-
// Ensure that cert manager is installed.
171-
if err := ensureCertManager(initOpts); err != nil {
172-
return fmt.Errorf("cannot ensure that cert manager is installed: %w", err)
173-
}
174-
175181
// Deploy CAPI operator if it doesn't exist.
176182
if !deploymentExists {
183+
log.Info("Installing CAPI operator", "Version", initOpts.operatorVersion)
184+
177185
if err := deployCAPIOperator(initOpts); err != nil {
178186
return fmt.Errorf("cannot deploy CAPI operator: %w", err)
179187
}
188+
189+
opts := wait.Backoff{
190+
Duration: 500 * time.Millisecond,
191+
Factor: 1.5,
192+
Steps: 10,
193+
Jitter: 0.4,
194+
}
195+
196+
log.Info("Waiting for CAPI Operator to be available...")
197+
198+
if err := wait.ExponentialBackoff(opts, func() (bool, error) {
199+
return checkCAPIOperatorAvailability(ctx, client)
200+
}); err != nil {
201+
return fmt.Errorf("cannot check CAPI operator availability: %w", err)
202+
}
203+
204+
log.Info("CAPI Operator is successfully installed.")
205+
} else {
206+
log.Info("Skipping installing CAPI Operator as it is already installed.")
180207
}
181208

182209
return initProviders(ctx, client, initOpts)
@@ -221,8 +248,8 @@ func initProviders(ctx context.Context, client ctrlclient.Client, initOpts *init
221248
return nil
222249
}
223250

224-
// checkCAPIOpearatorAvailability checks if the CAPI operator is available on the management cluster.
225-
func checkCAPIOpearatorAvailability(ctx context.Context, client ctrlclient.Client) (bool, error) {
251+
// checkCAPIOperatorAvailability checks if the CAPI operator is available on the management cluster.
252+
func checkCAPIOperatorAvailability(ctx context.Context, client ctrlclient.Client) (bool, error) {
226253
var deploymentList appsv1.DeploymentList
227254

228255
// Search deployments with desired labels in all namespaces.
@@ -234,7 +261,19 @@ func checkCAPIOpearatorAvailability(ctx context.Context, client ctrlclient.Clien
234261
return false, fmt.Errorf("more than one CAPI Operator deployments were found")
235262
}
236263

237-
return len(deploymentList.Items) == 1, nil
264+
if len(deploymentList.Items) == 0 {
265+
return false, nil
266+
}
267+
268+
deployment := deploymentList.Items[0]
269+
270+
for _, cond := range deployment.Status.Conditions {
271+
if cond.Type == appsv1.DeploymentAvailable && cond.Status == corev1.ConditionTrue {
272+
return true, nil
273+
}
274+
}
275+
276+
return false, nil
238277
}
239278

240279
func ensureCertManager(opts *initOptions) error {
@@ -378,16 +417,24 @@ func createGenericProvider(ctx context.Context, client ctrlclient.Client, provid
378417
spec := provider.GetSpec()
379418
spec.Version = version
380419
provider.SetSpec(spec)
420+
} else {
421+
version = "latest"
381422
}
382423

383424
// Ensure that desired namespace exists
384425
if err := EnsureNamespaceExists(ctx, client, namespace); err != nil {
385426
return fmt.Errorf("cannot ensure that namespace exists: %w", err)
386427
}
387428

429+
log.Info("Installing provider", "Type", provider.GetType(), "Name", name, "Version", version, "Namespace", namespace)
430+
388431
// Create the provider
389-
if err := client.Create(ctx, provider.GetObject()); err != nil && !apierrors.IsAlreadyExists(err) {
390-
return fmt.Errorf("cannot create provider: %w", err)
432+
if err := client.Create(ctx, provider.GetObject()); err != nil {
433+
if !apierrors.IsAlreadyExists(err) {
434+
return fmt.Errorf("cannot create provider: %w", err)
435+
}
436+
437+
log.Info("Provider already exists, skipping creation", "Type", provider.GetType(), "Name", name, "Version", version, "Namespace", namespace)
391438
}
392439

393440
return nil

cmd/plugin/cmd/init_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ func TestCheckCAPIOpearatorAvailability(t *testing.T) {
7171

7272
g.Expect(env.Create(ctx, deployment)).To(Succeed())
7373

74+
// Get created deployment and update its status
75+
g.Eventually(func() (bool, error) {
76+
err := env.Get(ctx, ctrlclient.ObjectKeyFromObject(deployment), deployment)
77+
if err != nil {
78+
return false, err
79+
}
80+
81+
return deployment != nil, nil
82+
}, waitShort).Should(BeTrue())
83+
84+
deployment.Status.Conditions = []appsv1.DeploymentCondition{
85+
{
86+
Type: appsv1.DeploymentAvailable,
87+
Status: corev1.ConditionTrue,
88+
Reason: "MinimumReplicasAvailable",
89+
Message: "Deployment has minimum availability.",
90+
},
91+
}
92+
93+
g.Expect(env.Status().Update(ctx, deployment)).To(Succeed())
94+
7495
g.Eventually(func() (bool, error) {
7596
deploymentFromServer := &appsv1.Deployment{}
7697
err := env.Get(ctx, ctrlclient.ObjectKeyFromObject(deployment), deploymentFromServer)
@@ -123,7 +144,7 @@ func TestCheckCAPIOpearatorAvailability(t *testing.T) {
123144
resources = append(resources, deploymentClone)
124145
}
125146

126-
available, err := checkCAPIOpearatorAvailability(ctx, env.Client)
147+
available, err := checkCAPIOperatorAvailability(ctx, env.Client)
127148

128149
if tt.wantErr {
129150
g.Expect(err).To(HaveOccurred())

cmd/plugin/cmd/root.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ import (
2222
"os"
2323
"strings"
2424

25+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
26+
2527
"github.com/MakeNowJust/heredoc"
2628
goerrors "github.com/go-errors/errors"
29+
"github.com/go-logr/logr"
2730
"github.com/spf13/cobra"
2831
)
2932

@@ -35,6 +38,8 @@ const (
3538

3639
var verbosity *int
3740

41+
var log logr.Logger
42+
3843
// RootCmd is capioperator root CLI command.
3944
var RootCmd = &cobra.Command{
4045
Use: "capioperator",
@@ -67,6 +72,9 @@ func init() {
6772

6873
verbosity = flag.CommandLine.Int("v", 0, "Set the log level verbosity. This overrides the CAPIOPERATOR_LOG_LEVEL environment variable.")
6974

75+
log = logf.NewLogger(logf.WithThreshold(verbosity))
76+
logf.SetLogger(log)
77+
7078
RootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
7179

7280
RootCmd.AddGroup(

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/MakeNowJust/heredoc v1.0.0
99
github.com/evanphx/json-patch/v5 v5.6.0
1010
github.com/go-errors/errors v1.4.2
11+
github.com/go-logr/logr v1.2.4
1112
github.com/google/go-cmp v0.6.0
1213
github.com/google/go-github/v52 v52.0.0
1314
github.com/google/gofuzz v1.2.0
@@ -48,7 +49,6 @@ require (
4849
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
4950
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
5051
github.com/fsnotify/fsnotify v1.6.0 // indirect
51-
github.com/go-logr/logr v1.2.4 // indirect
5252
github.com/go-openapi/jsonpointer v0.19.6 // indirect
5353
github.com/go-openapi/jsonreference v0.20.1 // indirect
5454
github.com/go-openapi/swag v0.22.3 // indirect

0 commit comments

Comments
 (0)