Skip to content

Commit 573194a

Browse files
authored
Merge pull request #1447 from exdx/feat/support-secrets
feat: support secrets from bundle
2 parents cd0626f + c3ed659 commit 573194a

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed

pkg/api/apis/operators/installplan_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ type StepResource struct {
354354
Kind string
355355
Name string
356356
Manifest string
357+
// BundleSecret is a one-off flag for handling secrets from a user bundle versus from the catalog source.
358+
// This field is handled internally by OLM and should not be exposed by the API.
359+
// Longer term StepResources will be refactored.
360+
BundleSecret bool
357361
}
358362

359363
func (r StepResource) String() string {

pkg/api/apis/operators/v1alpha1/installplan_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ type StepResource struct {
322322
Kind string `json:"kind"`
323323
Name string `json:"name"`
324324
Manifest string `json:"manifest,omitempty"`
325+
BundleSecret bool `json:"bundleSecret"`
325326
}
326327

327328
func (r StepResource) String() string {

pkg/api/apis/operators/v1alpha1/zz_generated.conversion.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/operators/catalog/operator.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,21 @@ func (o *Operator) ExecutePlan(plan *v1alpha1.InstallPlan) error {
15181518
plan.Status.Plan[i].Status = status
15191519

15201520
case secretKind:
1521+
if step.Resource.BundleSecret {
1522+
var s corev1.Secret
1523+
err := json.Unmarshal([]byte(step.Resource.Manifest), &s)
1524+
if err != nil {
1525+
return errorwrap.Wrapf(err, "error parsing step manifest: %s", step.Resource.Name)
1526+
}
1527+
status, err := ensurer.EnsureBundleSecret(plan.Namespace, &s)
1528+
if err != nil {
1529+
return err
1530+
}
1531+
1532+
plan.Status.Plan[i].Status = status
1533+
continue
1534+
}
1535+
15211536
status, err := ensurer.EnsureSecret(o.namespace, plan.GetNamespace(), step.Resource.Name)
15221537
if err != nil {
15231538
return err

pkg/controller/operators/catalog/operator_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,50 @@ func TestExecutePlan(t *testing.T) {
424424
want: []runtime.Object{configmap("cfg", namespace)},
425425
err: nil,
426426
},
427+
{
428+
testName: "CreateSecretFromBundle",
429+
in: withSteps(installPlan("p", namespace, v1alpha1.InstallPlanPhaseInstalling, "csv"),
430+
[]*v1alpha1.Step{
431+
{
432+
Resource: v1alpha1.StepResource{
433+
CatalogSource: "catalog",
434+
CatalogSourceNamespace: namespace,
435+
Group: "",
436+
Version: "v1",
437+
Kind: "Secret",
438+
Name: "s",
439+
Manifest: toManifest(t, secret("s", namespace)),
440+
BundleSecret: true,
441+
},
442+
Status: v1alpha1.StepStatusUnknown,
443+
},
444+
},
445+
),
446+
want: []runtime.Object{secret("s", namespace)},
447+
err: nil,
448+
},
449+
{
450+
testName: "DoesNotCreateSecretNotFromBundle",
451+
in: withSteps(installPlan("p", namespace, v1alpha1.InstallPlanPhaseInstalling, "csv"),
452+
[]*v1alpha1.Step{
453+
{
454+
Resource: v1alpha1.StepResource{
455+
CatalogSource: "catalog",
456+
CatalogSourceNamespace: namespace,
457+
Group: "",
458+
Version: "v1",
459+
Kind: "Secret",
460+
Name: "s",
461+
Manifest: toManifest(t, secret("s", namespace)),
462+
BundleSecret: false,
463+
},
464+
Status: v1alpha1.StepStatusUnknown,
465+
},
466+
},
467+
),
468+
want: []runtime.Object{},
469+
err: fmt.Errorf("secret s does not exist - secrets \"s\" not found"),
470+
},
427471
{
428472
testName: "UpdateServiceAccountWithSameFields",
429473
in: withSteps(installPlan("p", namespace, v1alpha1.InstallPlanPhaseInstalling, "csv"),
@@ -1388,6 +1432,15 @@ func service(name, namespace string) *corev1.Service {
13881432
}
13891433
}
13901434

1435+
func secret(name, namespace string) *corev1.Secret {
1436+
return &corev1.Secret{
1437+
ObjectMeta: metav1.ObjectMeta{
1438+
Name: name,
1439+
Namespace: namespace,
1440+
},
1441+
}
1442+
}
1443+
13911444
func serviceAccount(name, namespace, generateName string, secretRef *corev1.ObjectReference) *corev1.ServiceAccount {
13921445
if secretRef == nil {
13931446
return &corev1.ServiceAccount{

pkg/controller/operators/catalog/step_ensurer.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ func (o *StepEnsurer) EnsureSecret(operatorNamespace, planNamespace, name string
109109
return
110110
}
111111

112+
// EnsureBundleSecret creates user-specified secrets from the bundle. Called when StepResource.Secret is true
113+
func (o *StepEnsurer) EnsureBundleSecret(namespace string, secret *corev1.Secret) (status v1alpha1.StepStatus, err error) {
114+
_, createErr := o.kubeClient.KubernetesInterface().CoreV1().Secrets(namespace).Create(secret)
115+
if createErr == nil {
116+
status = v1alpha1.StepStatusCreated
117+
return
118+
}
119+
120+
if !k8serrors.IsAlreadyExists(createErr) {
121+
err = errorwrap.Wrapf(createErr, "error updating secret: %s", secret.GetName())
122+
return
123+
}
124+
125+
secret.SetNamespace(namespace)
126+
// NOTE: any annotations/changes applied to the secret are lost
127+
if _, updateErr := o.kubeClient.UpdateSecret(secret); updateErr != nil {
128+
err = errorwrap.Wrapf(updateErr, "error updating secret: %s", secret.GetName())
129+
return
130+
}
131+
132+
status = v1alpha1.StepStatusPresent
133+
return
134+
}
135+
112136
// EnsureServiceAccount writes the specified ServiceAccount object to the cluster.
113137
func (o *StepEnsurer) EnsureServiceAccount(namespace string, sa *corev1.ServiceAccount) (status v1alpha1.StepStatus, err error) {
114138
_, createErr := o.kubeClient.KubernetesInterface().CoreV1().ServiceAccounts(namespace).Create(sa)

pkg/controller/registry/resolver/steps.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
2121
)
2222

23+
const (
24+
secretKind = "Secret"
25+
)
26+
2327
var (
2428
scheme = runtime.NewScheme()
2529
)
@@ -71,6 +75,13 @@ func NewStepResourceFromObject(obj runtime.Object, catalogSourceName, catalogSou
7175
CatalogSourceNamespace: catalogSourceNamespace,
7276
}
7377

78+
// Treat secret objects with a special case
79+
// OLM copies secrets as well as supports creating new ones from the bundle
80+
// This boolean determines whether its a user-created secret
81+
if obj.GetObjectKind().GroupVersionKind().Kind == secretKind {
82+
resource.BundleSecret = true
83+
}
84+
7485
return resource, nil
7586
}
7687

@@ -124,6 +135,7 @@ func NewStepResourceFromBundle(bundle *api.Bundle, namespace, replaces, catalogS
124135
if unst.GetObjectKind().GroupVersionKind().Kind == v1alpha1.ClusterServiceVersionKind {
125136
continue
126137
}
138+
127139
step, err := NewStepResourceFromObject(unst, catalogSourceName, catalogSourceNamespace)
128140
if err != nil {
129141
return nil, err

0 commit comments

Comments
 (0)