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: start serving v2 API #132

Merged
merged 1 commit into from
Jun 10, 2024
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
29 changes: 29 additions & 0 deletions api/accurate/v2/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v2

import (
"testing"

accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
utilconversion "github.com/cybozu-go/accurate/internal/util/conversion"
fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
)

func TestFuzzyConversion(t *testing.T) {
t.Run("for SubNamespace", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &accuratev2alpha1.SubNamespace{},
Spoke: &SubNamespace{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{SubNamespaceStatusFuzzFunc},
}))
}

func SubNamespaceStatusFuzzFunc(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
SubNamespaceStatusFuzzer,
}
}

func SubNamespaceStatusFuzzer(in *SubNamespace, c fuzz.Continue) {
c.FuzzNoCustom(in)
}
1 change: 1 addition & 0 deletions api/accurate/v2/doc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// +kubebuilder:object:generate=true
// +groupName=accurate.cybozu.com
// +k8s:conversion-gen=github.com/cybozu-go/accurate/api/accurate/v2alpha1
package v2
2 changes: 2 additions & 0 deletions api/accurate/v2/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ var (

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme

localSchemeBuilder = SchemeBuilder.SchemeBuilder
)
43 changes: 43 additions & 0 deletions api/accurate/v2/subnamespace_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package v2

import (
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this SubNamespace to the Hub version (v2alpha1).
func (src *SubNamespace) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*accuratev2alpha1.SubNamespace)

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

return Convert_v2_SubNamespace_To_v2alpha1_SubNamespace(src, dst, nil)
}

// ConvertFrom converts from the Hub version (v2alpha1) to this version.
func (dst *SubNamespace) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*accuratev2alpha1.SubNamespace)

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

return Convert_v2alpha1_SubNamespace_To_v2_SubNamespace(src, dst, nil)
}

func getConversionLogger(obj client.Object) logr.Logger {
return ctrl.Log.WithName("conversion").WithValues(
"kind", "SubNamespace",
"namespace", obj.GetNamespace(),
"name", obj.GetName(),
)
}
1 change: 0 additions & 1 deletion api/accurate/v2/subnamespace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type SubNamespaceSpec struct {
}

//+kubebuilder:object:root=true
//+kubebuilder:unservedversion
//+kubebuilder:subresource:status
//+genclient

Expand Down
163 changes: 163 additions & 0 deletions api/accurate/v2/zz_generated.conversion.go

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

2 changes: 1 addition & 1 deletion charts/accurate/templates/generated/crds.yaml

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

4 changes: 4 additions & 0 deletions 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"
accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/controllers"
"github.com/cybozu-go/accurate/hooks"
Expand Down Expand Up @@ -44,6 +45,9 @@ func subMain(ns, addr string, port int) error {
if err := accuratev2alpha1.AddToScheme(scheme); err != nil {
return fmt.Errorf("unable to add Accurate v2alpha1 objects: %w", err)
}
if err := accuratev2.AddToScheme(scheme); err != nil {
return fmt.Errorf("unable to add Accurate v2 objects: %w", err)
}

cfgData, err := os.ReadFile(options.configFile)
if err != nil {
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"
accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -25,6 +26,9 @@ func makeClient(config *genericclioptions.ConfigFlags) (client.Client, error) {
if err := accuratev2alpha1.AddToScheme(scheme); err != nil {
return nil, err
}
if err := accuratev2.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.

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

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

//+kubebuilder:scaffold:scheme

Expand Down
2 changes: 1 addition & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ var _ = Describe("kubectl accurate", func() {
kubectlSafe(nil, "get", "subnamespaces", "-n", "subroot1", "sn1")

Eventually(func() string {
out, err := kubectl(nil, "get", "-n", "subroot2", "subnamespaces", "sn1", "-o", "json")
out, err := kubectl(nil, "get", "-n", "subroot2", "subnamespaces.v1.accurate.cybozu.com", "sn1", "-o", "json")
if err != nil {
return ""
}
Expand Down
3 changes: 2 additions & 1 deletion hooks/subnamespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"

accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/pkg/config"
"github.com/cybozu-go/accurate/pkg/constants"
Expand Down Expand Up @@ -140,7 +141,7 @@ 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{}} {
for _, s := range []runtime.Object{&accuratev1.SubNamespace{}, &accuratev2alpha1.SubNamespace{}, &accuratev2.SubNamespace{}} {
err := ctrl.NewWebhookManagedBy(mgr).
For(s).
Complete()
Expand Down
Loading
Loading