Skip to content

Commit

Permalink
Support IgnoreInvalidImageReferences
Browse files Browse the repository at this point in the history
When the image pruner's spec.ignoreInvalidImageReferences is true, the
pruner should be run with --ingore-invalid-refs=true.

This option is enabled by default for new instances.
  • Loading branch information
dmage committed Jul 31, 2020
1 parent 08c80f0 commit 8f99fa9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/operator/controllerimagepruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ func (c *ImagePrunerController) Bootstrap() error {
Namespace: defaults.ImageRegistryOperatorNamespace,
},
Spec: imageregistryv1.ImagePrunerSpec{
Suspend: &defaultPrunerSuspend,
KeepTagRevisions: &defaultPrunerKeepTagRevisions,
SuccessfulJobsHistoryLimit: &defaultPrunerSuccessfulJobsHistoryLimit,
FailedJobsHistoryLimit: &defaultPrunerFailedJobsHistoryLimit,
Suspend: &defaultPrunerSuspend,
KeepTagRevisions: &defaultPrunerKeepTagRevisions,
SuccessfulJobsHistoryLimit: &defaultPrunerSuccessfulJobsHistoryLimit,
FailedJobsHistoryLimit: &defaultPrunerFailedJobsHistoryLimit,
IgnoreInvalidImageReferences: true,
},
Status: imageregistryv1.ImagePrunerStatus{},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/resource/prunercronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (gcj *generatorPrunerCronJob) expected() (runtime.Object, error) {
"--certificate-authority=/var/run/configmaps/serviceca/service-ca.crt",
fmt.Sprintf("--keep-tag-revisions=%d", gcj.getKeepTagRevisions(cr)),
fmt.Sprintf("--keep-younger-than=%s", gcj.getKeepYoungerThan(cr)),
fmt.Sprintf("--ignore-invalid-refs=%t", cr.Spec.IgnoreInvalidImageReferences),
fmt.Sprintf("--prune-registry=%t", gcj.getPruneRegistry(rcr)),
"--confirm=true",
},
Expand Down
96 changes: 96 additions & 0 deletions test/e2e/pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import (
"github.com/openshift/cluster-image-registry-operator/test/framework"
)

func containsString(haystack []string, needle string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}
return false
}

// TestPruneRegistry ensures that the value for the --prune-registry flag
// is set correctly based on the image registry's custom resources
// Spec.ManagementState field
Expand Down Expand Up @@ -242,3 +251,90 @@ func TestPrunerPodCompletes(t *testing.T) {
t.Fatal(err)
}
}

func TestPrunerIgnoreInvalidImageReferences(t *testing.T) {
ctx := context.Background()
te := framework.SetupAvailableImageRegistry(t, nil)
defer framework.TeardownImageRegistry(te)

cr, err := te.Client().ImagePruners().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

origSpec := cr.Spec.DeepCopy()

suspend := false
cr.Spec.Suspend = &suspend
cr.Spec.Schedule = "* * * * *"
cr.Spec.IgnoreInvalidImageReferences = true
_, err = te.Client().ImagePruners().Update(
context.Background(), cr, metav1.UpdateOptions{},
)
if err != nil {
t.Fatal(err)
}
defer func() {
cr, err := te.Client().ImagePruners().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

cr.Spec = *origSpec

_, err = te.Client().ImagePruners().Update(ctx, cr, metav1.UpdateOptions{})
if err != nil {
t.Fatal(err)
}
}()

t.Logf("waiting the pruner config to be observed...")
err = wait.Poll(5*time.Second, framework.AsyncOperationTimeout, func() (stop bool, err error) {
cr, err := te.Client().ImagePruners().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return false, err
}

return cr.Status.ObservedGeneration == cr.Generation, nil
})
if err != nil {
t.Fatal(err)
}

cronjob, err := te.Client().CronJobs("openshift-image-registry").Get(ctx, "image-pruner", metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

if !containsString(cronjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Args, "--ignore-invalid-refs=true") {
defer framework.DumpYAML(t, "cronjob", cronjob)
t.Fatalf("flag --ignore-invalid-refs=true is not found")
}

t.Logf("waiting the pruner to succeed...")
err = wait.Poll(5*time.Second, framework.AsyncOperationTimeout, func() (stop bool, err error) {
pods, err := te.Client().Pods(defaults.ImageRegistryOperatorNamespace).List(ctx, metav1.ListOptions{})
if err != nil {
return false, err
}

for _, pod := range pods.Items {
if !strings.HasPrefix(pod.Name, "image-pruner-") {
continue
}
if !containsString(pod.Spec.Containers[0].Args, "--ignore-invalid-refs=true") {
// pod from another test?
t.Logf("pod %s has wrong arguments", pod.Name)
continue
}
t.Logf("%s: %s", pod.Name, pod.Status.Phase)
if pod.Status.Phase == "Succeeded" {
return true, nil
}
}
return false, nil
})
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 8f99fa9

Please sign in to comment.