Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Use podref package from quarks-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno authored and Mario Manno committed Sep 11, 2020
1 parent 65728c0 commit caf4dea
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 170 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module code.cloudfoundry.org/quarks-job

require (
code.cloudfoundry.org/quarks-utils v0.0.0-20200908102830-afb06ba35970
code.cloudfoundry.org/quarks-utils v0.0.0-20200911114130-7b6fe4c269e6
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/pkg/errors v0.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ bitbucket.org/liamstask/goose v0.0.0-20150115234039-8488cc47d90c/go.mod h1:hSVuE
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
code.cloudfoundry.org/quarks-utils v0.0.0-20200908102830-afb06ba35970 h1:/mPoPIFFmuoteCTjYhKLeekLUqjvbHD5oGGtZ35INNE=
code.cloudfoundry.org/quarks-utils v0.0.0-20200908102830-afb06ba35970/go.mod h1:Dlc5H34JWsNtMjdJYf7nm8hoxNl+hEQR0AMk0qOSmjc=
code.cloudfoundry.org/quarks-utils v0.0.0-20200911114130-7b6fe4c269e6 h1:e/ASQNPuSNTA+QRtpRrFG62FQ8WUdQeCV2UQE/j924I=
code.cloudfoundry.org/quarks-utils v0.0.0-20200911114130-7b6fe4c269e6/go.mod h1:Dlc5H34JWsNtMjdJYf7nm8hoxNl+hEQR0AMk0qOSmjc=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/controllers/quarksjob/errand_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func AddErrand(ctx context.Context, config *config.Config, mgr manager.Manager)
return []reconcile.Request{}
}

reconciles, err := reference.GetReconciles(ctx, mgr.GetClient(), reference.ReconcileForQuarksJob, cm)
reconciles, err := reference.GetReconciles(ctx, mgr.GetClient(), cm)
if err != nil {
ctxlog.Errorf(ctx, "Failed to calculate reconciles for config '%s/%s': %v", cm.Namespace, cm.Name, err)
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func AddErrand(ctx context.Context, config *config.Config, mgr manager.Manager)
return []reconcile.Request{}
}

reconciles, err := reference.GetReconciles(ctx, mgr.GetClient(), reference.ReconcileForQuarksJob, s)
reconciles, err := reference.GetReconciles(ctx, mgr.GetClient(), s)
if err != nil {
ctxlog.Errorf(ctx, "Failed to calculate reconciles for secret '%s/%s': %v", s.Namespace, s.Name, err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kube/controllers/quarksjob/job_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
crc "sigs.k8s.io/controller-runtime/pkg/client"

qjv1a1 "code.cloudfoundry.org/quarks-job/pkg/kube/apis/quarksjob/v1alpha1"
"code.cloudfoundry.org/quarks-job/pkg/kube/util/reference"
"code.cloudfoundry.org/quarks-utils/pkg/config"
"code.cloudfoundry.org/quarks-utils/pkg/ctxlog"
"code.cloudfoundry.org/quarks-utils/pkg/names"
"code.cloudfoundry.org/quarks-utils/pkg/podref"
vss "code.cloudfoundry.org/quarks-utils/pkg/versionedsecretstore"
)

Expand Down Expand Up @@ -165,7 +165,7 @@ func (j jobCreatorImpl) Create(ctx context.Context, qJob qjv1a1.QuarksJob) (bool
}

func (j jobCreatorImpl) validateReferences(ctx context.Context, qJob qjv1a1.QuarksJob) error {
configMaps := reference.ReferencedConfigMaps(qJob)
configMaps := podref.GetConfMapRefFromPod(qJob.Spec.Template.Spec.Template.Spec)
configMap := &corev1.ConfigMap{}
for configMapName := range configMaps {
if err := j.client.Get(ctx, crc.ObjectKey{Name: configMapName, Namespace: qJob.Namespace}, configMap); err != nil {
Expand All @@ -176,7 +176,7 @@ func (j jobCreatorImpl) validateReferences(ctx context.Context, qJob qjv1a1.Quar
}
}

secrets := reference.ReferencedSecrets(qJob)
secrets := podref.GetSecretRefFromPodSpec(qJob.Spec.Template.Spec.Template.Spec)
secret := &corev1.Secret{}
for secretName := range secrets {
if err := j.client.Get(ctx, crc.ObjectKey{Name: secretName, Namespace: qJob.Namespace}, secret); err != nil {
Expand Down
55 changes: 0 additions & 55 deletions pkg/kube/util/reference/configmap_references.go

This file was deleted.

75 changes: 23 additions & 52 deletions pkg/kube/util/reference/reconciles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package reference

import (
"context"
"fmt"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -13,27 +12,13 @@ import (
"code.cloudfoundry.org/quarks-job/pkg/kube/apis"
qjv1a1 "code.cloudfoundry.org/quarks-job/pkg/kube/apis/quarksjob/v1alpha1"
log "code.cloudfoundry.org/quarks-utils/pkg/ctxlog"
"code.cloudfoundry.org/quarks-utils/pkg/podref"
vss "code.cloudfoundry.org/quarks-utils/pkg/versionedsecretstore"
)

// ReconcileType lists all the types of reconciliations we can return,
// for controllers that have types that can reference ConfigMaps or Secrets
type ReconcileType int

const (
// ReconcileForQuarksJob represents the QuarksJob CRD
ReconcileForQuarksJob = iota
)

func (r ReconcileType) String() string {
return [...]string{
"QuarksJob",
}[r]
}

// GetReconciles returns reconciliation requests for the QuarksJobs
// that reference an object. The object can be a ConfigMap or a Secret
func GetReconciles(ctx context.Context, client crc.Client, reconcileType ReconcileType, object apis.Object) ([]reconcile.Request, error) {
func GetReconciles(ctx context.Context, client crc.Client, object apis.Object) ([]reconcile.Request, error) {
objReferencedBy := func(parent qjv1a1.QuarksJob) (bool, error) {
var (
objectReferences map[string]bool
Expand All @@ -44,10 +29,10 @@ func GetReconciles(ctx context.Context, client crc.Client, reconcileType Reconci

switch object := object.(type) {
case *corev1.ConfigMap:
objectReferences = ReferencedConfigMaps(parent)
objectReferences = podref.GetConfMapRefFromPod(parent.Spec.Template.Spec.Template.Spec)
name = object.Name
case *corev1.Secret:
objectReferences = ReferencedSecrets(parent)
objectReferences = podref.GetSecretRefFromPodSpec(parent.Spec.Template.Spec.Template.Spec)
name = object.Name
versionedSecret = vss.IsVersionedSecret(*object)
default:
Expand Down Expand Up @@ -76,32 +61,29 @@ func GetReconciles(ctx context.Context, client crc.Client, reconcileType Reconci
namespace := object.GetNamespace()
result := []reconcile.Request{}

switch reconcileType {
case ReconcileForQuarksJob:
quarksJobs, err := listQuarksJobs(ctx, client, namespace)
log.Debugf(ctx, "Searching QuarksJobs for references to '%s/%s' ", namespace, object.GetName())
quarksJobs := &qjv1a1.QuarksJobList{}
err := client.List(ctx, quarksJobs, crc.InNamespace(namespace))
if err != nil {
return nil, errors.Wrap(err, "failed to list QuarksJobs for reconciles")
}

for _, qJob := range quarksJobs.Items {
if !(qJob.Spec.UpdateOnConfigChange && qJob.IsAutoErrand()) {
continue
}
isRef, err := objReferencedBy(qJob)
if err != nil {
return nil, errors.Wrap(err, "failed to list QuarksJobs for reconciles")
return nil, err
}

for _, qJob := range quarksJobs.Items {
if !(qJob.Spec.UpdateOnConfigChange && qJob.IsAutoErrand()) {
continue
}
isRef, err := objReferencedBy(qJob)
if err != nil {
return nil, err
}

if isRef {
result = append(result, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: qJob.Name,
Namespace: qJob.Namespace,
}})
}
if isRef {
result = append(result, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: qJob.Name,
Namespace: qJob.Namespace,
}})
}
default:
return nil, fmt.Errorf("unknown reconcile type %s", reconcileType.String())
}

return result, nil
Expand Down Expand Up @@ -141,14 +123,3 @@ func SkipReconciles(ctx context.Context, client crc.Client, object apis.Object)
}
return false
}

func listQuarksJobs(ctx context.Context, client crc.Client, namespace string) (*qjv1a1.QuarksJobList, error) {
log.Debugf(ctx, "Listing QuarksJobs in namespace '%s'", namespace)
result := &qjv1a1.QuarksJobList{}
err := client.List(ctx, result, crc.InNamespace(namespace))
if err != nil {
return nil, errors.Wrap(err, "failed to list QuarksJobs")
}

return result, nil
}
55 changes: 0 additions & 55 deletions pkg/kube/util/reference/secret_references.go

This file was deleted.

0 comments on commit caf4dea

Please sign in to comment.