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

Move pkg/utils/crds to pkg/crd #1743

Merged
merged 1 commit into from
Jan 7, 2022
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 controllers/submariner/broker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
"github.com/pkg/errors"
"github.com/submariner-io/submariner-operator/api/submariner/v1alpha1"
"github.com/submariner-io/submariner-operator/pkg/broker"
"github.com/submariner-io/submariner-operator/pkg/crd"
"github.com/submariner-io/submariner-operator/pkg/discovery/globalnet"
"github.com/submariner-io/submariner-operator/pkg/gateway"
"github.com/submariner-io/submariner-operator/pkg/lighthouse"
crdutils "github.com/submariner-io/submariner-operator/pkg/utils/crds"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -80,7 +80,7 @@ func (r *BrokerReconciler) Reconcile(ctx context.Context, request ctrl.Request)
}

// Broker CRDs
crdUpdater := crdutils.NewFromControllerClient(r.Client)
crdUpdater := crd.UpdaterFromControllerClient(r.Client)

err = gateway.Ensure(crdUpdater)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions controllers/submariner/submariner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
submopv1a1 "github.com/submariner-io/submariner-operator/api/submariner/v1alpha1"
"github.com/submariner-io/submariner-operator/pkg/broker"
submarinerclientset "github.com/submariner-io/submariner-operator/pkg/client/clientset/versioned"
"github.com/submariner-io/submariner-operator/pkg/crd"
"github.com/submariner-io/submariner-operator/pkg/discovery/network"
"github.com/submariner-io/submariner-operator/pkg/gateway"
"github.com/submariner-io/submariner-operator/pkg/images"
crdutils "github.com/submariner-io/submariner-operator/pkg/utils/crds"
submv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -253,7 +253,7 @@ func getImagePath(submariner *submopv1a1.Submariner, imageName, componentName st

func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
// Set up the CRDs we need
crdUpdater, err := crdutils.NewFromRestConfig(mgr.GetConfig())
crdUpdater, err := crd.UpdaterFromRestConfig(mgr.GetConfig())
if err != nil {
return errors.Wrap(err, "error creating CRDUpdater")
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import (
submarinerv1alpha1 "github.com/submariner-io/submariner-operator/api/submariner/v1alpha1"
"github.com/submariner-io/submariner-operator/controllers"
"github.com/submariner-io/submariner-operator/controllers/submariner"
"github.com/submariner-io/submariner-operator/pkg/crd"
"github.com/submariner-io/submariner-operator/pkg/lighthouse"
"github.com/submariner-io/submariner-operator/pkg/metrics"
crdutils "github.com/submariner-io/submariner-operator/pkg/utils/crds"
"github.com/submariner-io/submariner-operator/pkg/version"
v1 "k8s.io/api/core/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -139,7 +139,7 @@ func main() {
log.Info("Registering Components.")

// Set up the CRDs we need
crdUpdater, err := crdutils.NewFromRestConfig(cfg)
crdUpdater, err := crd.UpdaterFromRestConfig(cfg)
if err != nil {
log.Error(err, "")
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions pkg/broker/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
"github.com/submariner-io/submariner-operator/internal/component"
"github.com/submariner-io/submariner-operator/internal/constants"
"github.com/submariner-io/submariner-operator/internal/rbac"
"github.com/submariner-io/submariner-operator/pkg/crd"
"github.com/submariner-io/submariner-operator/pkg/gateway"
"github.com/submariner-io/submariner-operator/pkg/lighthouse"
"github.com/submariner-io/submariner-operator/pkg/utils"
crdutils "github.com/submariner-io/submariner-operator/pkg/utils/crds"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -39,8 +39,8 @@ import (
"k8s.io/client-go/kubernetes"
)

func Ensure(crdUpdater crdutils.CRDUpdater, kubeClient kubernetes.Interface, componentArr []string, crds bool, namespace string) error {
if crds {
func Ensure(crdUpdater crd.Updater, kubeClient kubernetes.Interface, componentArr []string, createCRDs bool, namespace string) error {
if createCRDs {
for i := range componentArr {
switch componentArr[i] {
case component.Connectivity:
Expand Down
31 changes: 31 additions & 0 deletions pkg/crd/crd_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
SPDX-License-Identifier: Apache-2.0

Copyright Contributors to the Submariner project.

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 crd_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestCRDs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CRDs Suite")
}
135 changes: 135 additions & 0 deletions pkg/crd/updater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
SPDX-License-Identifier: Apache-2.0

Copyright Contributors to the Submariner project.

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.
*/

// nolint:wrapcheck // These functions are basically wrappers for the k8s APIs.
package crd

import (
"context"

"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/resource"
resourceutil "github.com/submariner-io/submariner-operator/pkg/resource"
"github.com/submariner-io/submariner-operator/pkg/subctl/operator/common/embeddedyamls"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type baseUpdater interface {
Create(context.Context, *apiextensions.CustomResourceDefinition, metav1.CreateOptions) (*apiextensions.CustomResourceDefinition, error)
Update(context.Context, *apiextensions.CustomResourceDefinition, metav1.UpdateOptions) (*apiextensions.CustomResourceDefinition, error)
Get(context.Context, string, metav1.GetOptions) (*apiextensions.CustomResourceDefinition, error)
Delete(context.Context, string, metav1.DeleteOptions) error
}

type Updater interface {
baseUpdater
CreateOrUpdate(context.Context, string) (bool, error)
}

type updater struct {
baseUpdater
}

type controllerClientCreator struct {
client client.Client
}

func UpdaterFromRestConfig(config *rest.Config) (Updater, error) {
apiext, err := clientset.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "error creating the api extensions client")
}

return UpdaterFromClientSet(apiext), nil
}

func UpdaterFromClientSet(cs clientset.Interface) Updater {
return &updater{baseUpdater: cs.ApiextensionsV1().CustomResourceDefinitions()}
}

func UpdaterFromControllerClient(controllerClient client.Client) Updater {
return &updater{baseUpdater: &controllerClientCreator{
client: controllerClient,
}}
}

func (u *updater) CreateOrUpdate(ctx context.Context, crdYaml string) (bool, error) {
crd := &apiextensions.CustomResourceDefinition{}

if err := embeddedyamls.GetObject(crdYaml, crd); err != nil {
return false, errors.Wrap(err, "error extracting embedded CRD")
}

return resourceutil.CreateOrUpdate(ctx, &resource.InterfaceFuncs{
GetFunc: func(ctx context.Context, name string, options metav1.GetOptions) (runtime.Object, error) {
return u.Get(ctx, name, options)
},
CreateFunc: func(ctx context.Context, obj runtime.Object, options metav1.CreateOptions) (runtime.Object, error) {
return u.Create(ctx, obj.(*apiextensions.CustomResourceDefinition), options)
},
UpdateFunc: func(ctx context.Context, obj runtime.Object, options metav1.UpdateOptions) (runtime.Object, error) {
return u.Update(ctx, obj.(*apiextensions.CustomResourceDefinition), options)
},
}, crd)
}

func (c *controllerClientCreator) Create(ctx context.Context, crd *apiextensions.CustomResourceDefinition,
options metav1.CreateOptions) (*apiextensions.CustomResourceDefinition, error) {
// TODO skitt handle options
err := c.client.Create(ctx, crd)
return crd, err
}

func (c *controllerClientCreator) Update(ctx context.Context, crd *apiextensions.CustomResourceDefinition,
options metav1.UpdateOptions) (*apiextensions.CustomResourceDefinition, error) {
// TODO skitt handle options
err := c.client.Update(ctx, crd)
return crd, err
}

func (c *controllerClientCreator) Get(ctx context.Context, name string,
options metav1.GetOptions) (*apiextensions.CustomResourceDefinition, error) {
crd := &apiextensions.CustomResourceDefinition{}
// TODO skitt handle options
err := c.client.Get(ctx, client.ObjectKey{Name: name}, crd)
if err != nil {
return nil, err
}

return crd, nil
}

func (c *controllerClientCreator) Delete(ctx context.Context, name string,
options metav1.DeleteOptions) error { // nolint:gocritic // Match K8s API
crd, err := c.Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}

if crd == nil {
return nil
}

// TODO skitt handle options
return c.client.Delete(ctx, crd)
}
105 changes: 105 additions & 0 deletions pkg/crd/updater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
SPDX-License-Identifier: Apache-2.0

Copyright Contributors to the Submariner project.

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 crd_test

import (
"context"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/submariner-io/submariner-operator/pkg/crd"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
extendedfakeclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
crdYAML = `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: submariners.submariner.io
spec:
group: submariner.io
names:
kind: Submariner
`
)

var _ = Describe("Updater", func() {
var (
client *extendedfakeclientset.Clientset
updater crd.Updater
)

BeforeEach(func() {
client = extendedfakeclientset.NewSimpleClientset()
updater = crd.UpdaterFromClientSet(client)
})

assertCRDExists := func(name string) {
crd, err := updater.Get(context.TODO(), name, metav1.GetOptions{})
Expect(err).To(Succeed())
Expect(crd.Spec.Names.Kind).Should(Equal("Submariner"))
}

Context("on CreateOrUpdate", func() {
crd := &apiextensions.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: "CustomResourceDefinition",
APIVersion: apiextensions.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "submariners.submariner.io",
},
Spec: apiextensions.CustomResourceDefinitionSpec{
Group: "submariner.io",
Names: apiextensions.CustomResourceDefinitionNames{
Kind: "Submariner",
},
},
}

When("the CRD doesn't exist", func() {
It("should create it", func() {
created, err := updater.CreateOrUpdate(context.TODO(), crdYAML)
Expect(created).To(BeTrue())
Expect(err).To(Succeed())
assertCRDExists(crd.Name)
})
})

When("the CRD already exists", func() {
It("should not update it", func() {
_, err := updater.Create(context.TODO(), crd, metav1.CreateOptions{})
Expect(err).To(Succeed())
assertCRDExists(crd.Name)

created, err := updater.CreateOrUpdate(context.TODO(), crdYAML)
Expect(created).To(BeFalse())
Expect(err).To(Succeed())

actualActions := client.Actions()
for i := range actualActions {
Expect(actualActions[i].GetVerb()).ToNot(Equal("update"))
}
})
})
})
})
4 changes: 2 additions & 2 deletions pkg/deploy/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
"github.com/submariner-io/submariner-operator/pkg/broker"
"github.com/submariner-io/submariner-operator/pkg/brokercr"
"github.com/submariner-io/submariner-operator/pkg/client"
"github.com/submariner-io/submariner-operator/pkg/crd"
"github.com/submariner-io/submariner-operator/pkg/discovery/globalnet"
"github.com/submariner-io/submariner-operator/pkg/reporter"
"github.com/submariner-io/submariner-operator/pkg/subctl/operator/submarinerop"
crdutils "github.com/submariner-io/submariner-operator/pkg/utils/crds"
v1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -84,7 +84,7 @@ func Broker(options *BrokerOptions, clientProducer client.Producer, status repor
func deploy(options *BrokerOptions, status reporter.Interface, clientProducer client.Producer) error {
status.Start("Setting up broker RBAC")

err := broker.Ensure(crdutils.NewFromClientSet(clientProducer.ForCRD()), clientProducer.ForKubernetes(),
err := broker.Ensure(crd.UpdaterFromClientSet(clientProducer.ForCRD()), clientProducer.ForKubernetes(),
options.BrokerSpec.Components, false, options.BrokerNamespace)
if err != nil {
return status.Error(err, "error setting up broker RBAC")
Expand Down
Loading