Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 462c4c1

Browse files
committed
overall refactoring and linter fixes
1 parent 88dbeb6 commit 462c4c1

18 files changed

+2064
-2707
lines changed

Dockerfile

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ARG ARCH
2828
# It's an invalid finding since the image is explicitly set in the Makefile.
2929
# https://github.com/hadolint/hadolint/wiki/DL3006
3030
# hadolint ignore=DL3006
31-
FROM ${builder_image} as builder
31+
FROM ${builder_image} AS builder
3232
WORKDIR /workspace
3333

3434
# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy
@@ -71,34 +71,31 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
7171
# Production image
7272
FROM ${deployment_base_image}:${deployment_base_image_tag}
7373

74+
# Set shell with pipefail option for better error handling
75+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
76+
7477
# Install Node.js and cdk8s-cli directly
78+
# hadolint ignore=DL3015
7579
RUN apt-get update && \
76-
apt-get install -y ca-certificates curl wget && \
80+
apt-get install -y --no-install-recommends ca-certificates=20240203~22.04.1 curl=7.81.0-1ubuntu1.20 && \
7781
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
78-
apt-get install -y nodejs && \
79-
npm install -g cdk8s-cli && \
80-
wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz && \
81-
tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz && \
82-
rm go1.21.5.linux-amd64.tar.gz && \
82+
apt-get install -y nodejs=18.19.1-1nodesource1 && \
83+
npm install -g cdk8s-cli@2.200.96 && \
84+
curl -fsSL -o go1.24.4.linux-amd64.tar.gz https://go.dev/dl/go1.24.4.linux-amd64.tar.gz && \
85+
tar -C /usr/local -xzf go1.24.4.linux-amd64.tar.gz && \
86+
rm go1.24.4.linux-amd64.tar.gz && \
8387
apt-get clean && \
8488
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
8589

86-
# Set Go environment variables
90+
# Set Go environment variables
8791
ENV PATH=$PATH:/usr/local/go/bin
8892
ENV GOROOT=/usr/local/go
8993

90-
RUN cdk8s --version && go version
91-
9294
WORKDIR /
9395
COPY --from=builder /workspace/manager .
9496

9597
# Create non-root user
9698
RUN useradd --uid 65532 --create-home --shell /bin/bash nonroot
97-
98-
# Ensure the non-root user can access Go by setting up their environment
99-
RUN echo 'export PATH=$PATH:/usr/local/go/bin' >> /home/nonroot/.bashrc && \
100-
echo 'export GOROOT=/usr/local/go' >> /home/nonroot/.bashrc && \
101-
echo 'export PATH=$PATH:/usr/local/go/bin' >> /home/nonroot/.profile
10299

103100
# Switch back to non-root user (this line should already exist)
104101
# USER root # This was part of the removed direct install, ensure it's not re-added here unless needed for COPY permissions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ CAPI_KIND_CLUSTER_NAME ?= capi-test
232232

233233
# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971
234234

235-
TAG ?= v0.2.4-alpha
235+
TAG ?= v0.2.6-preview
236236
ARCH ?= $(shell go env GOARCH)
237237
ALL_ARCH = amd64 arm arm64
238238

api/v1alpha1/cdk8sappproxy_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type GitRepositorySpec struct {
4141
// containing authentication credentials for the Git repository.
4242
// The secret must contain 'username' and 'password' fields.
4343
// +kubebuilder:validation:Optional
44-
AuthSecretRef *corev1.LocalObjectReference `json:"authSecretRef,omitempty"` // New field
44+
AuthSecretRef *corev1.LocalObjectReference `json:"authSecretRef,omitempty"`
4545
}
4646

4747
// Cdk8sAppProxySpec defines the desired state of Cdk8sAppProxy.

api/v1alpha1/cdk8sappproxy_webhook_test.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package v1alpha1
22

33
import (
4-
"context"
54
"testing"
65

76
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -90,12 +89,7 @@ func TestCdk8sAppProxy_Default(t *testing.T) {
9089

9190
for _, tt := range tests {
9291
t.Run(tt.name, func(t *testing.T) {
93-
ctx := context.Background()
94-
err := tt.proxy.Default(ctx, tt.proxy)
95-
if err != nil {
96-
t.Errorf("Default() error = %v", err)
97-
return
98-
}
92+
tt.proxy.Default()
9993

10094
if tt.proxy.Spec.GitRepository != nil && tt.expected.Spec.GitRepository != nil {
10195
if tt.proxy.Spec.GitRepository.Reference != tt.expected.Spec.GitRepository.Reference {
@@ -177,21 +171,19 @@ func TestCdk8sAppProxy_ValidateCreate(t *testing.T) {
177171

178172
for _, tt := range tests {
179173
t.Run(tt.name, func(t *testing.T) {
180-
ctx := context.Background()
181-
warnings, err := tt.proxy.ValidateCreate(ctx, tt.proxy)
174+
warnings, err := tt.proxy.ValidateCreate()
182175

183176
if tt.wantError {
184177
if err == nil {
185178
t.Errorf("ValidateCreate() expected error, got nil")
179+
186180
return
187181
}
188182
if tt.errorMsg != "" && err.Error() != "validation failed: ["+tt.errorMsg+"]" {
189183
t.Errorf("ValidateCreate() error = %v, want error containing %v", err.Error(), tt.errorMsg)
190184
}
191-
} else {
192-
if err != nil {
193-
t.Errorf("ValidateCreate() unexpected error = %v", err)
194-
}
185+
} else if err != nil {
186+
t.Errorf("ValidateCreate() unexpected error = %v", err)
195187
}
196188

197189
// Warnings should always be nil in our implementation
@@ -248,8 +240,7 @@ func TestCdk8sAppProxy_ValidateUpdate(t *testing.T) {
248240

249241
for _, tt := range tests {
250242
t.Run(tt.name, func(t *testing.T) {
251-
ctx := context.Background()
252-
warnings, err := tt.newProxy.ValidateUpdate(ctx, tt.oldProxy, tt.newProxy)
243+
warnings, err := tt.newProxy.ValidateUpdate(tt.oldProxy)
253244

254245
if tt.wantError {
255246
if err == nil {
@@ -278,8 +269,7 @@ func TestCdk8sAppProxy_ValidateDelete(t *testing.T) {
278269
},
279270
}
280271

281-
ctx := context.Background()
282-
warnings, err := proxy.ValidateDelete(ctx, proxy)
272+
warnings, err := proxy.ValidateDelete()
283273

284274
if err != nil {
285275
t.Errorf("ValidateDelete() unexpected error = %v", err)
@@ -335,15 +325,17 @@ func TestCdk8sAppProxy_validateCdk8sAppProxy(t *testing.T) {
335325

336326
if (err != nil) != tt.wantError {
337327
t.Errorf("validateCdk8sAppProxy() error = %v, wantError %v", err, tt.wantError)
328+
338329
return
339330
}
340331

341332
// Compare warnings properly
342-
if tt.wantWarnings == nil && warnings != nil {
333+
switch {
334+
case tt.wantWarnings == nil && warnings != nil:
343335
t.Errorf("validateCdk8sAppProxy() warnings = %v, want nil", warnings)
344-
} else if tt.wantWarnings != nil && warnings == nil {
336+
case tt.wantWarnings != nil && warnings == nil:
345337
t.Errorf("validateCdk8sAppProxy() warnings = nil, want %v", tt.wantWarnings)
346-
} else if tt.wantWarnings != nil && warnings != nil {
338+
case tt.wantWarnings != nil && warnings != nil:
347339
if len(warnings) != len(tt.wantWarnings) {
348340
t.Errorf("validateCdk8sAppProxy() warnings length = %d, want %d", len(warnings), len(tt.wantWarnings))
349341
} else {

api/v1alpha1/cdk8sappproxy_webook.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ func (r *Cdk8sAppProxy) SetupWebhookWithManager(mgr ctrl.Manager) error {
2121

2222
// +kubebuilder:webhook:path=/mutate-addons-cluster-x-k8s-io-v1alpha1-cdk8sappproxy,mutating=true,failurePolicy=fail,sideEffects=None,groups=addons.cluster.x-k8s.io,resources=cdk8sappproxies,verbs=create;update,versions=v1alpha1,name=cdk8sappproxy.kb.io,admissionReviewVersions=v1
2323

24-
// var _ webhook.CustomDefaulter = &Cdk8sAppProxy{}
24+
// var _ webhook.CustomDefaulter = &Cdk8sAppProxy{}.
2525
var _ webhook.Defaulter = &Cdk8sAppProxy{}
2626

27-
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
27+
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type.
2828
func (r *Cdk8sAppProxy) Default() {
2929
cdk8sappproxylog.Info("default", "name", r.Name)
3030

@@ -43,21 +43,21 @@ func (r *Cdk8sAppProxy) Default() {
4343

4444
var _ webhook.Validator = &Cdk8sAppProxy{}
4545

46-
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
46+
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
4747
func (r *Cdk8sAppProxy) ValidateCreate() (admission.Warnings, error) {
4848
cdk8sappproxylog.Info("validate create", "name", r.Name)
4949

5050
return r.validateCdk8sAppProxy()
5151
}
5252

53-
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
53+
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
5454
func (r *Cdk8sAppProxy) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
5555
cdk8sappproxylog.Info("validate update", "name", r.Name)
5656

5757
return r.validateCdk8sAppProxy()
5858
}
5959

60-
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
60+
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
6161
func (r *Cdk8sAppProxy) ValidateDelete() (admission.Warnings, error) {
6262
cdk8sappproxylog.Info("validate delete", "name", r.Name)
6363

api/v1alpha1/webhook_suite_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"context"
21+
"crypto/tls"
22+
"fmt"
23+
"net"
24+
"path/filepath"
25+
"testing"
26+
"time"
27+
28+
. "github.com/onsi/ginkgo/v2"
29+
. "github.com/onsi/gomega"
30+
admissionv1beta1 "k8s.io/api/admission/v1beta1"
31+
"k8s.io/apimachinery/pkg/runtime"
32+
ctrl "sigs.k8s.io/controller-runtime"
33+
"sigs.k8s.io/controller-runtime/pkg/client"
34+
"sigs.k8s.io/controller-runtime/pkg/envtest"
35+
logf "sigs.k8s.io/controller-runtime/pkg/log"
36+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
37+
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
38+
"sigs.k8s.io/controller-runtime/pkg/webhook"
39+
)
40+
41+
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
42+
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
43+
44+
var (
45+
k8sClient client.Client
46+
testEnv *envtest.Environment
47+
ctx context.Context
48+
cancel context.CancelFunc
49+
)
50+
51+
func TestAPIs(t *testing.T) {
52+
RegisterFailHandler(Fail)
53+
54+
RunSpecs(t, "Webhook Suite")
55+
}
56+
57+
var _ = BeforeSuite(func() {
58+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
59+
60+
ctx, cancel = context.WithCancel(context.TODO())
61+
62+
By("bootstrapping test environment")
63+
testEnv = &envtest.Environment{
64+
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
65+
ErrorIfCRDPathMissing: false,
66+
WebhookInstallOptions: envtest.WebhookInstallOptions{
67+
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
68+
},
69+
}
70+
71+
cfg, err := testEnv.Start()
72+
Expect(err).NotTo(HaveOccurred())
73+
Expect(cfg).NotTo(BeNil())
74+
75+
scheme := runtime.NewScheme()
76+
err = AddToScheme(scheme)
77+
Expect(err).NotTo(HaveOccurred())
78+
79+
err = admissionv1beta1.AddToScheme(scheme)
80+
Expect(err).NotTo(HaveOccurred())
81+
82+
err = admissionv1beta1.AddToScheme(scheme)
83+
Expect(err).NotTo(HaveOccurred())
84+
85+
//+kubebuilder:scaffold:scheme
86+
87+
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
88+
Expect(err).NotTo(HaveOccurred())
89+
Expect(k8sClient).NotTo(BeNil())
90+
91+
// start webhook server using Manager
92+
webhookInstallOptions := &testEnv.WebhookInstallOptions
93+
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
94+
Scheme: scheme,
95+
WebhookServer: webhook.NewServer(
96+
webhook.Options{
97+
Host: webhookInstallOptions.LocalServingHost,
98+
Port: webhookInstallOptions.LocalServingPort,
99+
CertDir: webhookInstallOptions.LocalServingCertDir,
100+
},
101+
),
102+
LeaderElection: false,
103+
Metrics: metricsserver.Options{BindAddress: "0"},
104+
})
105+
Expect(err).NotTo(HaveOccurred())
106+
107+
err = (&Cdk8sAppProxy{}).SetupWebhookWithManager(mgr)
108+
Expect(err).NotTo(HaveOccurred())
109+
110+
//+kubebuilder:scaffold:webhook
111+
112+
go func() {
113+
defer GinkgoRecover()
114+
err = mgr.Start(ctx)
115+
Expect(err).NotTo(HaveOccurred())
116+
}()
117+
118+
// wait for the webhook server to get ready
119+
dialer := &net.Dialer{Timeout: time.Second}
120+
addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort)
121+
Eventually(func() error {
122+
conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true})
123+
if err != nil {
124+
return err
125+
}
126+
127+
return conn.Close()
128+
}).Should(Succeed())
129+
})
130+
131+
var _ = AfterSuite(func() {
132+
cancel()
133+
By("tearing down the test environment")
134+
err := testEnv.Stop()
135+
Expect(err).NotTo(HaveOccurred())
136+
})

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.2.4-alpha
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.2.6-preview
1111
name: manager

config/default/manager_image_patch.yaml-e

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.2.4-alpha
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.2.6-preview
1111
name: manager

config/default/manager_pull_policy.yaml-e

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ spec:
88
spec:
99
containers:
1010
- name: manager
11-
imagePullPolicy: IfNotPresent
11+
imagePullPolicy: Always

0 commit comments

Comments
 (0)