Skip to content

Commit

Permalink
Use generic resource handling
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Kitt <[email protected]>
  • Loading branch information
skitt committed Oct 8, 2023
1 parent 769aad7 commit c823e23
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 123 deletions.
42 changes: 21 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/submariner-io/admiral v0.16.0-m4
github.com/submariner-io/cloud-prepare v0.16.0-m4
github.com/submariner-io/lighthouse v0.16.0-m4
github.com/submariner-io/admiral v0.16.0-m4.0.20231006142904-4e92b35176d1
github.com/submariner-io/cloud-prepare v0.16.0-m4.0.20231006141729-2ef04b485fa8
github.com/submariner-io/lighthouse v0.16.0-m4.0.20231006142050-32037a51c84b
github.com/submariner-io/shipyard v0.16.0-m4
github.com/submariner-io/submariner v0.16.0-m4.0.20230908080904-ea10edbd6d96
github.com/submariner-io/submariner-operator v0.16.0-m4.0.20231006081605-d5bc46c3f242
github.com/submariner-io/submariner v0.16.0-m4.0.20231006143928-7ffa96a74e93
github.com/submariner-io/submariner-operator v0.16.0-m4.0.20231006145554-2b1da7742e6b
github.com/uw-labs/lichen v0.1.7
golang.org/x/net v0.15.0
golang.org/x/oauth2 v0.12.0
google.golang.org/api v0.138.0
k8s.io/api v0.28.1
google.golang.org/api v0.143.0
k8s.io/api v0.28.2
k8s.io/apiextensions-apiserver v0.28.1
k8s.io/apimachinery v0.28.1
k8s.io/client-go v0.28.1
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.16.1
sigs.k8s.io/controller-runtime v0.16.2
sigs.k8s.io/mcs-api v0.1.0
sigs.k8s.io/yaml v1.3.0
)
Expand All @@ -46,17 +46,17 @@ require (
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/aws/aws-sdk-go-v2 v1.21.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.38 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.36 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.42 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.40 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.42 // indirect
github.com/aws/aws-sdk-go-v2/service/ec2 v1.109.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.43 // indirect
github.com/aws/aws-sdk-go-v2/service/ec2 v1.122.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.13.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.21.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.14.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.17.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.22.0 // indirect
github.com/aws/smithy-go v1.14.2 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
Expand All @@ -79,11 +79,11 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/licenseclassifier v0.0.0-20201113175434-78a70215ca36 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/s2a-go v0.1.5 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.1 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gophercloud/gophercloud v1.6.0 // indirect
github.com/gophercloud/gophercloud v1.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imdario/mergo v0.3.13 // indirect
Expand Down Expand Up @@ -117,7 +117,7 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
124 changes: 48 additions & 76 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/brokercr/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Ensure(ctx context.Context, client controllerClient.Client, namespace strin
Spec: brokerSpec,
}

_, err := util.CreateAnew(ctx, resource.ForControllerClient(client, namespace, &submariner.Broker{}), brokerCR,
_, err := util.CreateAnew[*submariner.Broker](ctx, resource.ForControllerClient(client, namespace, &submariner.Broker{}), brokerCR,
metav1.CreateOptions{}, metav1.DeleteOptions{})

return errors.Wrap(err, "error creating Broker resource")
Expand Down
7 changes: 2 additions & 5 deletions pkg/namespace/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,24 @@ import (
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
)

// Ensure functions updates or installs the operator CRDs in the cluster.
func Ensure(ctx context.Context, kubeClient kubernetes.Interface, namespace string, namespaceLabels map[string]string) (bool, error) {
ns := &v1.Namespace{ObjectMeta: v1meta.ObjectMeta{Name: namespace, Labels: namespaceLabels}}

_, err := util.CreateOrUpdate(ctx, resource.ForNamespace(kubeClient), ns, func(existing runtime.Object) (runtime.Object,
_, err := util.CreateOrUpdate[*v1.Namespace](ctx, resource.ForNamespace(kubeClient), ns, func(ns *v1.Namespace) (*v1.Namespace,
error,
) {
ns := existing.(*v1.Namespace)

if ns.Labels == nil {
ns.Labels = map[string]string{}
}

for k, v := range namespaceLabels {
ns.Labels[k] = v
}
return existing, nil
return ns, nil
})

if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/resource/create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

func CreateOrUpdate(ctx context.Context, client resource.Interface, obj runtime.Object) (bool, error) {
func CreateOrUpdate[T runtime.Object](ctx context.Context, client resource.Interface[T], obj T) (bool, error) {
result, err := util.CreateOrUpdate(ctx, client, obj, util.Replace(obj))
return result == util.OperationResultCreated, err //nolint:wrapcheck // No need to wrap.
}
9 changes: 4 additions & 5 deletions pkg/secret/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ import (
"github.com/submariner-io/admiral/pkg/util"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
)

func Ensure(ctx context.Context, client kubernetes.Interface, namespace string, secret *v1.Secret) (*v1.Secret, error) {
//nolint:wrapcheck // No need to wrap errors here
object, err := util.CreateAnew(ctx, &resource.InterfaceFuncs{
GetFunc: func(ctx context.Context, name string, options metav1.GetOptions) (runtime.Object, error) {
object, err := util.CreateAnew[*v1.Secret](ctx, &resource.InterfaceFuncs[*v1.Secret]{
GetFunc: func(ctx context.Context, name string, options metav1.GetOptions) (*v1.Secret, error) {
return client.CoreV1().Secrets(namespace).Get(ctx, name, options)
},
CreateFunc: func(ctx context.Context, obj runtime.Object, options metav1.CreateOptions) (runtime.Object, error) {
return client.CoreV1().Secrets(namespace).Create(ctx, obj.(*v1.Secret), options)
CreateFunc: func(ctx context.Context, obj *v1.Secret, options metav1.CreateOptions) (*v1.Secret, error) {
return client.CoreV1().Secrets(namespace).Create(ctx, obj, options)
},
DeleteFunc: func(ctx context.Context, name string, options metav1.DeleteOptions) error {
return client.CoreV1().Secrets(namespace).Delete(ctx, name, options)
Expand Down
5 changes: 2 additions & 3 deletions pkg/serviceaccount/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
Expand All @@ -45,8 +44,8 @@ const (

func ensure(ctx context.Context, kubeClient kubernetes.Interface, namespace string, sa *corev1.ServiceAccount) (bool, error) {
result, err := util.CreateOrUpdate(ctx, resource.ForServiceAccount(kubeClient, namespace), sa,
func(existing runtime.Object) (runtime.Object, error) {
existing.(*corev1.ServiceAccount).Secrets = nil
func(existing *corev1.ServiceAccount) (*corev1.ServiceAccount, error) {
existing.Secrets = nil
return existing, nil
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/servicediscoverycr/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Ensure(ctx context.Context,
Spec: *serviceDiscoverySpec,
}

_, err := resourceutil.CreateOrUpdate(ctx, resource.ForControllerClient(client, namespace,
_, err := resourceutil.CreateOrUpdate[*operatorv1alpha1.ServiceDiscovery](ctx, resource.ForControllerClient(client, namespace,
&operatorv1alpha1.ServiceDiscovery{}), sd)

return errors.Wrap(err, "error creating/updating ServiceDiscovery resource")
Expand Down
3 changes: 2 additions & 1 deletion pkg/submarinercr/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func Ensure(ctx context.Context, client controllerClient.Client, namespace strin

propagationPolicy := metav1.DeletePropagationForeground

_, err := util.CreateAnew(ctx, resource.ForControllerClient(client, namespace, &operatorv1alpha1.Submariner{}),
_, err := util.CreateAnew[*operatorv1alpha1.Submariner](ctx,
resource.ForControllerClient(client, namespace, &operatorv1alpha1.Submariner{}),
submarinerCR, metav1.CreateOptions{}, metav1.DeleteOptions{PropagationPolicy: &propagationPolicy})

return errors.Wrap(err, "error creating Submariner resource")
Expand Down
17 changes: 8 additions & 9 deletions pkg/uninstall/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
controller "sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -108,18 +107,18 @@ func unlabelGatewayNodes(clients client.Producer, clusterName string, status rep
}

//nolint:wrapcheck // Let the caller wrap errors
nodeInterface := &resource.InterfaceFuncs{
GetFunc: func(ctx context.Context, name string, options metav1.GetOptions) (runtime.Object, error) {
nodeInterface := &resource.InterfaceFuncs[*corev1.Node]{
GetFunc: func(ctx context.Context, name string, options metav1.GetOptions) (*corev1.Node, error) {
return clients.ForKubernetes().CoreV1().Nodes().Get(ctx, name, options)
},
UpdateFunc: func(ctx context.Context, obj runtime.Object, options metav1.UpdateOptions) (runtime.Object, error) {
return clients.ForKubernetes().CoreV1().Nodes().Update(ctx, obj.(*corev1.Node), options)
UpdateFunc: func(ctx context.Context, obj *corev1.Node, options metav1.UpdateOptions) (*corev1.Node, error) {
return clients.ForKubernetes().CoreV1().Nodes().Update(ctx, obj, options)
},
}

for i := range list.Items {
err = util.Update(context.TODO(), nodeInterface, &list.Items[i], func(existing runtime.Object) (runtime.Object, error) {
delete(existing.(*corev1.Node).Labels, constants.SubmarinerGatewayLabel)
err = util.Update[*corev1.Node](context.TODO(), nodeInterface, &list.Items[i], func(existing *corev1.Node) (*corev1.Node, error) {
delete(existing.Labels, constants.SubmarinerGatewayLabel)
return existing, nil
})
if err != nil {
Expand Down Expand Up @@ -298,10 +297,10 @@ func ensureDeleted(clients client.Producer, obj controller.Object, status report
"the resource will be force-deleted", podStatusStr)
}

err = finalizer.Remove(context.TODO(), resource.ForControllerClient(clients.ForGeneral(), obj.GetNamespace(), obj),
err = finalizer.Remove[controller.Object](context.TODO(), resource.ForControllerClient(clients.ForGeneral(), obj.GetNamespace(), obj),
obj, opnames.CleanupFinalizer)
if err != nil {
return err //nolint:wrapcheck // No need to wrap
return err
}

return awaitDeleted()
Expand Down

0 comments on commit c823e23

Please sign in to comment.