Skip to content

Commit

Permalink
Merge pull request #339 from zqq454224016/main
Browse files Browse the repository at this point in the history
feat: change OCI URL
  • Loading branch information
bjwswang authored Sep 12, 2023
2 parents 1d38f5c + b927297 commit 2ad4cde
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 7 deletions.
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ resources:
kind: Repository
path: github.com/kubebb/core/api/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
webhookVersion: v1
- api:
crdVersion: v1
controller: true
Expand Down
8 changes: 4 additions & 4 deletions api/v1alpha1/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ const (
)

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

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

// IsOCI determines whether the repository is to be treated as an OCI repo
func (repo *Repository) IsOCI() bool {
return registry.IsOCI(repo.Spec.URL)
func (r *Repository) IsOCI() bool {
return registry.IsOCI(r.Spec.URL)
}
56 changes: 56 additions & 0 deletions api/v1alpha1/repository_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2023 The Kubebb Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"
"strings"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var repositorylog = logf.Log.WithName("repository-resource")

func (r *Repository) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithDefaulter(r).
Complete()
}

//+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

var _ webhook.CustomDefaulter = &Repository{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Repository) Default(ctx context.Context, obj runtime.Object) error {
log := repositorylog.WithValues("name", r.Name, "method", "Default")
p, ok := obj.(*Repository)
if !ok {
log.Error(ErrDecode, ErrDecode.Error())
return ErrDecode
}
if p.IsOCI() {
p.Spec.URL = strings.ToLower(p.Spec.URL)
}
log.Info("set default value done")
return nil
}
3 changes: 3 additions & 0 deletions api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ var _ = BeforeSuite(func() {
err = (&Portal{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&Repository{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down
2 changes: 1 addition & 1 deletion config/samples/core_v1alpha1_repository_oci_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: repository-oci-sample
namespace: kubebb-system
spec:
url: oci://registry-1.docker.io/abirdcfly
url: oci://registry-1.docker.io/Abirdcfly
pullStategy:
intervalSeconds: 600
retry: 5
6 changes: 6 additions & 0 deletions config/samples/example-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ info "7 try to verify that the common steps are valid to oci types"
info "7.1 create oci repository"
kubectl apply -f config/samples/core_v1alpha1_repository_oci_test.yaml
waitComponentStatus "kubebb-system" "repository-oci-sample.nginx"
oldurl="oci://registry-1.docker.io/Abirdcfly"
newurl=$(kubectl get repo repository-oci-sample -nkubebb-system -ojson | jq -r '.spec.url')
if [[ ${oldurl} == ${newurl} ]]; then
echo "invalid repository url"
exit 1
fi
oci_digest=$(kubectl -nkubebb-system get components repository-oci-sample.nginx -ojson | jq -r '.status.versions[] | select(.version == "15.1.0") | .digest')
fixed_nginx_digest="d9459e1206a4f5a8e0d7c5da8a306ab9b1ba5d7182ae671610b5699250ea45f8"
echo "digest: ${oci_digest}"
Expand Down
20 changes: 20 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ webhooks:
resources:
- subscriptions
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-core-kubebb-k8s-com-cn-v1alpha1-repository
failurePolicy: Fail
name: mrepository.kb.io
rules:
- apiGroups:
- core.kubebb.k8s.com.cn
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- repositories
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ func main() {
setupLog.Error(err, "unable to create webhook", "webhook", "Component")
os.Exit(1)
}
if err = (&corev1alpha1.Repository{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Repository")
os.Exit(1)
}
}
if corev1alpha1.RatingEnabled() {
if err = (&controllers.RatingReconciler{
Expand All @@ -201,8 +205,6 @@ func main() {
}
}

//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
Expand Down

0 comments on commit 2ad4cde

Please sign in to comment.