Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable v2alpha1 API with conversion webhook #107

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/accurate/v1/subnamespace_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (src *SubNamespace) ConvertTo(dstRaw conversion.Hub) error {

logger := getConversionLogger(src).WithValues(
"source", SchemeGroupVersion.Version,
"destination", SchemeGroupVersion.Version,
"destination", accuratev2alpha1.SchemeGroupVersion.Version,
)
logger.V(5).Info("converting")

Expand Down Expand Up @@ -57,7 +57,7 @@ func (dst *SubNamespace) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*accuratev2alpha1.SubNamespace)

logger := getConversionLogger(src).WithValues(
"source", SchemeGroupVersion.Version,
"source", accuratev2alpha1.SchemeGroupVersion.Version,
"destination", SchemeGroupVersion.Version,
)
logger.V(5).Info("converting")
Expand Down
2 changes: 0 additions & 2 deletions api/accurate/v2alpha1/subnamespace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ type SubNamespaceSpec struct {
Annotations map[string]string `json:"annotations,omitempty"`
}

// Keeping this version un-served for now
//+kubebuilder:unservedversion
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+genclient
Expand Down
15 changes: 13 additions & 2 deletions charts/accurate/templates/generated/crds.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion cmd/accurate-controller/sub/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"

accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/controllers"
"github.com/cybozu-go/accurate/hooks"
"github.com/cybozu-go/accurate/pkg/config"
Expand All @@ -35,7 +36,10 @@ func subMain(ns, addr string, port int) error {
return fmt.Errorf("unable to add client-go objects: %w", err)
}
if err := accuratev1.AddToScheme(scheme); err != nil {
return fmt.Errorf("unable to add Accurate objects: %w", err)
return fmt.Errorf("unable to add Accurate v1 objects: %w", err)
}
if err := accuratev2alpha1.AddToScheme(scheme); err != nil {
return fmt.Errorf("unable to add Accurate v2alpha1 objects: %w", err)
}

cfgData, err := os.ReadFile(options.configFile)
Expand Down
4 changes: 4 additions & 0 deletions cmd/kubectl-accurate/sub/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sub

import (
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -21,6 +22,9 @@ func makeClient(config *genericclioptions.ConfigFlags) (client.Client, error) {
if err := accuratev1.AddToScheme(scheme); err != nil {
return nil, err
}
if err := accuratev2alpha1.AddToScheme(scheme); err != nil {
return nil, err
}

return client.New(cfg, client.Options{Scheme: scheme})
}
2 changes: 1 addition & 1 deletion config/crd/bases/accurate.cybozu.com_subnamespaces.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ resources:
#+kubebuilder:scaffold:crdkustomizeresource

patchesStrategicMerge:
- patches/cainjection_in_subnamespaces.yaml
- patches/fix-crd.yaml
- patches/webhook_in_subnamespaces.yaml

# the following config is for teaching kustomize how to do kustomization for CRDs.
configurations:
- kustomizeconfig.yaml
19 changes: 19 additions & 0 deletions config/crd/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file is for teaching kustomize how to substitute name and namespace reference in CRD
nameReference:
- kind: Service
version: v1
fieldSpecs:
- kind: CustomResourceDefinition
version: v1
group: apiextensions.k8s.io
path: spec/conversion/webhook/clientConfig/service/name

namespace:
- kind: CustomResourceDefinition
version: v1
group: apiextensions.k8s.io
path: spec/conversion/webhook/clientConfig/service/namespace
create: false

varReference:
- path: metadata/annotations
7 changes: 7 additions & 0 deletions config/crd/patches/cainjection_in_subnamespaces.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
name: subnamespaces.accurate.cybozu.com
16 changes: 16 additions & 0 deletions config/crd/patches/webhook_in_subnamespaces.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The following patch enables a conversion webhook for the CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: subnamespaces.accurate.cybozu.com
spec:
conversion:
strategy: Webhook
webhook:
clientConfig:
service:
name: webhook-service
namespace: system
path: /convert
conversionReviewVersions:
- v1
13 changes: 13 additions & 0 deletions config/kustomize-to-helm/overlays/crds/crd_conversion_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
cert-manager.io/inject-ca-from: '{{ .Release.Namespace }}/{{ template "accurate.fullname" . }}-serving-cert'
name: subnamespaces.accurate.cybozu.com
spec:
conversion:
webhook:
clientConfig:
service:
name: '{{ template "accurate.fullname" . }}-webhook-service'
namespace: '{{ .Release.Namespace }}'
4 changes: 2 additions & 2 deletions config/kustomize-to-helm/overlays/crds/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
resources:
- ../../../crd

commonLabels:
app.kubernetes.io/name: accurate
patchesStrategicMerge:
- crd_conversion_patch.yaml

components:
- ../../components/common-labels
3 changes: 3 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/pkg/config"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/cybozu-go/accurate/pkg/feature"
Expand Down Expand Up @@ -70,6 +71,8 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
err = accuratev1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
err = accuratev2alpha1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

Expand Down
8 changes: 8 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ var _ = Describe("kubectl accurate", func() {
}).Should(Succeed())
})

It("should convert SubNamespace to new version", func() {
kubectlSafe(nil, "create", "ns", "rootv2")
kubectlSafe(nil, "accurate", "ns", "set-type", "rootv2", "root")
kubectlSafe(nil, "accurate", "sub", "create", "subv2", "rootv2")
kubectlSafe(nil, "get", "subnamespaces", "-n", "rootv2", "subv2")
kubectlSafe(nil, "get", "subnamespaces.v2alpha1.accurate.cybozu.com", "-n", "rootv2", "subv2")
})

It("should run other commands", func() {
kubectlSafe(nil, "accurate", "list")
kubectlSafe(nil, "accurate", "sub", "list")
Expand Down
12 changes: 12 additions & 0 deletions hooks/subnamespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"regexp"

accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/pkg/config"
"github.com/cybozu-go/accurate/pkg/constants"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1annotationvalidation "k8s.io/apimachinery/pkg/api/validation"
v1labelvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -137,6 +140,15 @@ func (v *subNamespaceValidator) notMatchingNamingPolicy(ctx context.Context, ns,

// SetupSubNamespaceWebhook registers the webhooks for SubNamespace
func SetupSubNamespaceWebhook(mgr manager.Manager, dec *admission.Decoder, namingPolicyRegexps []config.NamingPolicyRegexp) error {
for _, s := range []runtime.Object{&accuratev1.SubNamespace{}, &accuratev2alpha1.SubNamespace{}} {
err := ctrl.NewWebhookManagedBy(mgr).
For(s).
Complete()
if err != nil {
return err
}
}

serv := mgr.GetWebhookServer()

m := &subNamespaceMutator{
Expand Down
3 changes: 3 additions & 0 deletions hooks/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
admissionv1beta1 "k8s.io/api/admission/v1beta1"
//+kubebuilder:scaffold:imports
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/pkg/config"
"github.com/cybozu-go/accurate/pkg/indexing"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -64,6 +65,8 @@ var _ = BeforeSuite(func() {
scheme := runtime.NewScheme()
err = accuratev1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
err = accuratev2alpha1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
err = clientgoscheme.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down