-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also move the separate CRD CreateOrUpdate functions from utils to the Updater interface. Signed-off-by: Tom Pantelis <[email protected]>
- Loading branch information
Showing
17 changed files
with
343 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.