Skip to content

Commit

Permalink
Upgrade SDK to K8s 1.26, turn down verbosity of logs
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Jan 24, 2023
1 parent 212e60f commit aebc8dd
Show file tree
Hide file tree
Showing 10 changed files with 917 additions and 284 deletions.
2 changes: 1 addition & 1 deletion config/crd/patches/cainjection_in_clusterpullsecrets.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1beta1
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
Expand Down
2 changes: 1 addition & 1 deletion config/crd/patches/webhook_in_clusterpullsecrets.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The following patch enables conversion webhook for CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1beta1
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: clusterpullsecrets.ops.alexellis.io
Expand Down
8 changes: 3 additions & 5 deletions controllers/clusterpullsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"

"github.com/go-logr/logr"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -38,21 +37,20 @@ type ClusterPullSecretReconciler struct {

// Reconcile applies a number of ClusterPullSecrets to the default ServiceAccount
// within various valid namespaces. Namespaces can be ignored as required.
func (r *ClusterPullSecretReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
func (r *ClusterPullSecretReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("clusterpullsecret", req.NamespacedName)

var pullSecret v1.ClusterPullSecret
if err := r.Get(ctx, req.NamespacedName, &pullSecret); err != nil {
r.Log.Info(fmt.Sprintf("%s\n", errors.Wrap(err, "unable to fetch pullSecret")))
r.Log.Info(fmt.Sprintf("unable to fetch pullSecret %s, error: %s", req.NamespacedName, err))
} else {

r.Log.Info(fmt.Sprintf("Found: %s\n", pullSecret.Name))

namespaces := &corev1.NamespaceList{}
r.Client.List(ctx, namespaces)

r.Log.Info(fmt.Sprintf("Found %d namespaces", len(namespaces.Items)))
r.Log.V(10).Info(fmt.Sprintf("Found %d namespaces", len(namespaces.Items)))

for _, namespace := range namespaces.Items {
namespaceName := namespace.Name
Expand Down
8 changes: 3 additions & 5 deletions controllers/namespace_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"

"github.com/go-logr/logr"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -29,17 +28,16 @@ type NamespaceWatcher struct {
// +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=namespaces/status,verbs=get;update;patch

func (r *NamespaceWatcher) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
func (r *NamespaceWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Log.WithValues("namespace", req.NamespacedName)

var namespace corev1.Namespace
if err := r.Get(ctx, req.NamespacedName, &namespace); err != nil {
r.Log.Info(fmt.Sprintf("%s", errors.Wrap(err, "unable to fetch pullSecret")))
r.Log.Info(fmt.Sprintf("unable to fetch pullSecret %s, error: %s", req.NamespacedName, err))
return ctrl.Result{}, nil
}

r.Log.Info(fmt.Sprintf("detected a change in namespace: %s", namespace.Name))
r.Log.V(10).Info(fmt.Sprintf("detected a change in namespace: %s", namespace.Name))

pullSecretList := &opsv1.ClusterPullSecretList{}
err := r.Client.List(ctx, pullSecretList)
Expand Down
6 changes: 2 additions & 4 deletions controllers/secret_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (r *SecretReconciler) Reconcile(clusterPullSecret v1.ClusterPullSecret, ns
return nil
}

r.Log.Info(fmt.Sprintf("Getting SA for: %s", ns))
r.Log.V(10).Info(fmt.Sprintf("Getting SA for: %s", ns))

if clusterPullSecret.Spec.SecretRef == nil ||
clusterPullSecret.Spec.SecretRef.Name == "" ||
Expand Down Expand Up @@ -120,8 +120,6 @@ func (r *SecretReconciler) createSecret(clusterPullSecret v1.ClusterPullSecret,
return errors.Wrap(err, "unexpected error checking for the namespaced pull secret")
}

r.Log.Info(fmt.Sprintf("secret not found: %s.%s, %s", secretKey, ns, err.Error()))

nsSecret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretKey,
Expand Down Expand Up @@ -161,7 +159,7 @@ func (r *SecretReconciler) appendSecretToSA(clusterPullSecret v1.ClusterPullSecr
return wrappedErr
}

r.Log.Info(fmt.Sprintf("Pull secrets: %v", sa.ImagePullSecrets))
r.Log.V(10).Info(fmt.Sprintf("Pull secrets: %v", sa.ImagePullSecrets))

hasSecret := hasImagePullSecret(sa, secretKey)

Expand Down
10 changes: 4 additions & 6 deletions controllers/serviceaccount_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ type ServiceAccountWatcher struct {
// +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=serviceaccounts/status,verbs=get;update;patch

func (r *ServiceAccountWatcher) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
func (r *ServiceAccountWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Log.WithValues("serviceaccount", req.NamespacedName)

var sa corev1.ServiceAccount
Expand All @@ -50,7 +49,7 @@ func (r *ServiceAccountWatcher) Reconcile(req ctrl.Request) (ctrl.Result, error)
return ctrl.Result{}, nil
}

r.Log.Info(fmt.Sprintf("detected a change in serviceaccount: %s", sa.Name))
r.Log.V(10).Info(fmt.Sprintf("detected a change in serviceaccount: %s", sa.Name))

pullSecretList := &v1.ClusterPullSecretList{}
err := r.Client.List(ctx, pullSecretList)
Expand Down Expand Up @@ -84,7 +83,7 @@ func (r *ServiceAccountWatcher) appendSecretToSA(clusterPullSecret v1.ClusterPul
return wrappedErr
}

r.Log.Info(fmt.Sprintf("Pull secrets: %v", sa.ImagePullSecrets))
r.Log.V(10).Info(fmt.Sprintf("Pull secrets: %v", sa.ImagePullSecrets))

hasSecret := hasImagePullSecret(sa, secretKey)

Expand All @@ -93,8 +92,7 @@ func (r *ServiceAccountWatcher) appendSecretToSA(clusterPullSecret v1.ClusterPul
Name: secretKey,
})

err = r.Update(ctx, sa.DeepCopy())
if err != nil {
if err = r.Update(ctx, sa.DeepCopy()); err != nil {
wrappedErr := fmt.Errorf("unable to append pull secret to service account: %s", err)
r.Log.Info(wrappedErr.Error())
return err
Expand Down
17 changes: 8 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ module alexellis/registry-creds
go 1.13

require (
github.com/go-logr/logr v0.1.0
github.com/google/martian v2.1.0+incompatible
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/pkg/errors v0.8.1
k8s.io/api v0.17.2
k8s.io/apimachinery v0.17.2
k8s.io/client-go v0.17.2
sigs.k8s.io/controller-runtime v0.5.0
github.com/go-logr/logr v1.2.3
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.26.0 // indirect
github.com/pkg/errors v0.9.1
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
sigs.k8s.io/controller-runtime v0.14.1
)

0 comments on commit aebc8dd

Please sign in to comment.