Skip to content

Commit

Permalink
feat(init): add init container to olm operator that can clean previous
Browse files Browse the repository at this point in the history
deployments

CVO doesn't delete manifests during upgrades, so we need to clean up
after older releases.
  • Loading branch information
ecordell committed Jul 8, 2019
1 parent a61428c commit f03cdcd
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 21 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LABEL io.openshift.release.operator=true
COPY --from=builder /build/bin/olm /bin/olm
COPY --from=builder /build/bin/catalog /bin/catalog
COPY --from=builder /build/bin/package-server /bin/package-server
COPY --from=builder /build/bin/init /bin/init

# This image doesn't need to run as root user.
USER 1001
Expand Down
128 changes: 128 additions & 0 deletions cmd/init/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package main

import (
"context"
"flag"
"time"

log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/signals"
)

var (
kubeConfigPath = flag.String(
"kubeconfig", "", "absolute path to the kubeconfig file")
namespace = flag.String(
"namespace", "", "namespace where init runs")
crc versioned.Interface
c operatorclient.ClientInterface
)

const (
pollInterval = 1 * time.Second
pollDuration = 5 * time.Minute
)

type checkResourceFunc func() error
type deleteResourceFunc func() error

func main() {
// Get exit signal context
ctx, cancel := context.WithCancel(signals.Context())
defer cancel()

logger := log.New()

// Parse the command-line flags.
flag.Parse()

if client, err := client.NewClient(*kubeConfigPath); err != nil {
logger.WithError(err).Fatalf("error configuring client")
} else {
crc = client
}

c = operatorclient.NewClientFromConfig(*kubeConfigPath, logger)

if err := waitForDelete(checkCatalogSource("olm-operators"), deleteCatalogSource("olm-operators")); err != nil {
log.WithError(err).Fatal("couldn't clean previous release")
}

if err := waitForDelete(checkConfigMap("olm-operators"), deleteConfigMap("olm-operators")); err != nil {
log.WithError(err).Fatal("couldn't clean previous release")
}

if err := waitForDelete(checkSubscription("packageserver"), deleteSubscription("packageserver")); err != nil {
log.WithError(err).Fatal("couldn't clean previous release")
}

ctx.Done()
}

func checkSubscription(name string) checkResourceFunc {
return func() error {
_, err := crc.OperatorsV1alpha1().Subscriptions(*namespace).Get(name, metav1.GetOptions{})
return err
}
}

func deleteSubscription(name string) deleteResourceFunc {
return func() error {
return crc.OperatorsV1alpha1().Subscriptions(*namespace).Delete(name, metav1.NewDeleteOptions(0))
}
}

func checkConfigMap(name string) checkResourceFunc {
return func() error {
_, err := c.KubernetesInterface().CoreV1().ConfigMaps(*namespace).Get(name, metav1.GetOptions{})
return err
}
}

func deleteConfigMap(name string) deleteResourceFunc {
return func() error {
return c.KubernetesInterface().CoreV1().ConfigMaps(*namespace).Delete(name, metav1.NewDeleteOptions(0))
}
}

func checkCatalogSource(name string) checkResourceFunc {
return func() error {
_, err := crc.OperatorsV1alpha1().CatalogSources(*namespace).Get(name, metav1.GetOptions{})
return err
}
}

func deleteCatalogSource(name string) deleteResourceFunc {
return func() error {
return crc.OperatorsV1alpha1().CatalogSources(*namespace).Delete(name, metav1.NewDeleteOptions(0))
}
}

func waitForDelete(checkResource checkResourceFunc, deleteResource deleteResourceFunc) error {
if err := checkResource(); err != nil && errors.IsNotFound(err) {
return nil
}
if err := deleteResource(); err != nil {
return err
}
var err error
err = wait.Poll(pollInterval, pollDuration, func() (bool, error) {
err := checkResource()
if errors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
return false, nil
})

return err
}
3 changes: 2 additions & 1 deletion cmd/olm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ func main() {
olm.WithRestConfig(config),
)
if err != nil {
log.Fatalf("error configuring operator: %s", err.Error())
log.WithError(err).Fatalf("error configuring operator")
return
}

op.Run(ctx)
Expand Down
14 changes: 14 additions & 0 deletions deploy/chart/templates/0000_50_olm_07-olm-operator.deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ spec:
{{- if and .Values.installType (eq .Values.installType "ocp") }}
priorityClassName: "system-cluster-critical"
{{- end }}
initContainers:
- name: init
command:
- /bin/init
- -namespace
- ${NAMESPACE}
image: {{ .Values.olm.image.ref }}
imagePullPolicy: {{ .Values.olm.image.pullPolicy }}
terminationMessagePolicy: FallbackToLogsOnError
env:
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
containers:
- name: olm-operator
command:
Expand Down
1 change: 1 addition & 0 deletions e2e.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ WORKDIR /
COPY --from=builder /go/src/github.com/operator-framework/operator-lifecycle-manager/bin/olm /bin/olm
COPY --from=builder /go/src/github.com/operator-framework/operator-lifecycle-manager/bin/catalog /bin/catalog
COPY --from=builder /go/src/github.com/operator-framework/operator-lifecycle-manager/bin/package-server /bin/package-server
COPY --from=builder /go/src/github.com/operator-framework/operator-lifecycle-manager/build/bin/init /bin/init
EXPOSE 8080
EXPOSE 5443
CMD ["/bin/olm"]
1 change: 1 addition & 0 deletions local.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ WORKDIR /
COPY olm /bin/olm
COPY catalog /bin/catalog
COPY package-server /bin/package-server
COPY init /bin/init
EXPOSE 8080
EXPOSE 5443
CMD ["/bin/olm"]
14 changes: 14 additions & 0 deletions manifests/0000_50_olm_07-olm-operator.deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ spec:
spec:
serviceAccountName: olm-operator-serviceaccount
priorityClassName: "system-cluster-critical"
initContainers:
- name: init
command:
- /bin/init
- -namespace
- ${NAMESPACE}
image: quay.io/operator-framework/olm@sha256:f965474776bada158e4bf7be5c84b54460843e7478f06060990d2fdeb31b0b90
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
env:
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
containers:
- name: olm-operator
command:
Expand Down
Loading

0 comments on commit f03cdcd

Please sign in to comment.