Skip to content

Commit 2ad4cde

Browse files
authored
Merge pull request #339 from zqq454224016/main
feat: change OCI URL
2 parents 1d38f5c + b927297 commit 2ad4cde

File tree

8 files changed

+97
-7
lines changed

8 files changed

+97
-7
lines changed

PROJECT

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ resources:
2020
kind: Repository
2121
path: github.com/kubebb/core/api/v1alpha1
2222
version: v1alpha1
23+
webhooks:
24+
defaulting: true
25+
webhookVersion: v1
2326
- api:
2427
crdVersion: v1
2528
controller: true

api/v1alpha1/repository.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ const (
5959
)
6060

6161
// NamespacedName return the namespaced name of the repository in string format
62-
func (repo *Repository) NamespacedName() string {
63-
return fmt.Sprintf("%s.%s", repo.GetNamespace(), repo.GetName())
62+
func (r *Repository) NamespacedName() string {
63+
return fmt.Sprintf("%s.%s", r.GetNamespace(), r.GetName())
6464
}
6565

6666
// IsPullStrategySame Determine whether the contents of two structures are the same
@@ -215,6 +215,6 @@ func ParseRepoSecret(c client.Client, instance *Repository) (username, password,
215215
}
216216

217217
// IsOCI determines whether the repository is to be treated as an OCI repo
218-
func (repo *Repository) IsOCI() bool {
219-
return registry.IsOCI(repo.Spec.URL)
218+
func (r *Repository) IsOCI() bool {
219+
return registry.IsOCI(r.Spec.URL)
220220
}

api/v1alpha1/repository_webhook.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2023 The Kubebb 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+
"strings"
22+
23+
"k8s.io/apimachinery/pkg/runtime"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
logf "sigs.k8s.io/controller-runtime/pkg/log"
26+
"sigs.k8s.io/controller-runtime/pkg/webhook"
27+
)
28+
29+
// log is for logging in this package.
30+
var repositorylog = logf.Log.WithName("repository-resource")
31+
32+
func (r *Repository) SetupWebhookWithManager(mgr ctrl.Manager) error {
33+
return ctrl.NewWebhookManagedBy(mgr).
34+
For(r).
35+
WithDefaulter(r).
36+
Complete()
37+
}
38+
39+
//+kubebuilder:webhook:path=/mutate-core-kubebb-k8s-com-cn-v1alpha1-repository,mutating=true,failurePolicy=fail,sideEffects=None,groups=core.kubebb.k8s.com.cn,resources=repositories,verbs=create;update,versions=v1alpha1,name=mrepository.kb.io,admissionReviewVersions=v1
40+
41+
var _ webhook.CustomDefaulter = &Repository{}
42+
43+
// Default implements webhook.Defaulter so a webhook will be registered for the type
44+
func (r *Repository) Default(ctx context.Context, obj runtime.Object) error {
45+
log := repositorylog.WithValues("name", r.Name, "method", "Default")
46+
p, ok := obj.(*Repository)
47+
if !ok {
48+
log.Error(ErrDecode, ErrDecode.Error())
49+
return ErrDecode
50+
}
51+
if p.IsOCI() {
52+
p.Spec.URL = strings.ToLower(p.Spec.URL)
53+
}
54+
log.Info("set default value done")
55+
return nil
56+
}

api/v1alpha1/webhook_suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ var _ = BeforeSuite(func() {
108108
err = (&Portal{}).SetupWebhookWithManager(mgr)
109109
Expect(err).NotTo(HaveOccurred())
110110

111+
err = (&Repository{}).SetupWebhookWithManager(mgr)
112+
Expect(err).NotTo(HaveOccurred())
113+
111114
//+kubebuilder:scaffold:webhook
112115

113116
go func() {

config/samples/core_v1alpha1_repository_oci_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: repository-oci-sample
55
namespace: kubebb-system
66
spec:
7-
url: oci://registry-1.docker.io/abirdcfly
7+
url: oci://registry-1.docker.io/Abirdcfly
88
pullStategy:
99
intervalSeconds: 600
1010
retry: 5

config/samples/example-test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ info "7 try to verify that the common steps are valid to oci types"
571571
info "7.1 create oci repository"
572572
kubectl apply -f config/samples/core_v1alpha1_repository_oci_test.yaml
573573
waitComponentStatus "kubebb-system" "repository-oci-sample.nginx"
574+
oldurl="oci://registry-1.docker.io/Abirdcfly"
575+
newurl=$(kubectl get repo repository-oci-sample -nkubebb-system -ojson | jq -r '.spec.url')
576+
if [[ ${oldurl} == ${newurl} ]]; then
577+
echo "invalid repository url"
578+
exit 1
579+
fi
574580
oci_digest=$(kubectl -nkubebb-system get components repository-oci-sample.nginx -ojson | jq -r '.status.versions[] | select(.version == "15.1.0") | .digest')
575581
fixed_nginx_digest="d9459e1206a4f5a8e0d7c5da8a306ab9b1ba5d7182ae671610b5699250ea45f8"
576582
echo "digest: ${oci_digest}"

config/webhook/manifests.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ webhooks:
8585
resources:
8686
- subscriptions
8787
sideEffects: None
88+
- admissionReviewVersions:
89+
- v1
90+
clientConfig:
91+
service:
92+
name: webhook-service
93+
namespace: system
94+
path: /mutate-core-kubebb-k8s-com-cn-v1alpha1-repository
95+
failurePolicy: Fail
96+
name: mrepository.kb.io
97+
rules:
98+
- apiGroups:
99+
- core.kubebb.k8s.com.cn
100+
apiVersions:
101+
- v1alpha1
102+
operations:
103+
- CREATE
104+
- UPDATE
105+
resources:
106+
- repositories
107+
sideEffects: None
88108
---
89109
apiVersion: admissionregistration.k8s.io/v1
90110
kind: ValidatingWebhookConfiguration

main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ func main() {
190190
setupLog.Error(err, "unable to create webhook", "webhook", "Component")
191191
os.Exit(1)
192192
}
193+
if err = (&corev1alpha1.Repository{}).SetupWebhookWithManager(mgr); err != nil {
194+
setupLog.Error(err, "unable to create webhook", "webhook", "Repository")
195+
os.Exit(1)
196+
}
193197
}
194198
if corev1alpha1.RatingEnabled() {
195199
if err = (&controllers.RatingReconciler{
@@ -201,8 +205,6 @@ func main() {
201205
}
202206
}
203207

204-
//+kubebuilder:scaffold:builder
205-
206208
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
207209
setupLog.Error(err, "unable to set up health check")
208210
os.Exit(1)

0 commit comments

Comments
 (0)