Skip to content

Commit

Permalink
fix(installplans): add ability to apply Services
Browse files Browse the repository at this point in the history
  • Loading branch information
njhale committed Jan 18, 2019
1 parent 4d6a1e5 commit faf835f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/controller/operators/catalog/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
clusterRoleKind = "ClusterRole"
clusterRoleBindingKind = "ClusterRoleBinding"
serviceAccountKind = "ServiceAccount"
serviceKind = "Service"
roleKind = "Role"
roleBindingKind = "RoleBinding"
)
Expand Down Expand Up @@ -1031,6 +1032,41 @@ func (o *Operator) ExecutePlan(plan *v1alpha1.InstallPlan) error {
// If no error occurred, mark the step as Created.
plan.Status.Plan[i].Status = v1alpha1.StepStatusCreated
}

case serviceKind:
// Marshal the manifest into a Service instance
var s corev1.Service
err := json.Unmarshal([]byte(step.Resource.Manifest), &s)
if err != nil {
return errorwrap.Wrapf(err, "error parsing step manifest: %s", step.Resource.Name)
}

// Update UIDs on all CSV OwnerReferences
updated, err := o.getUpdatedOwnerReferences(s.OwnerReferences, plan.Namespace)
if err != nil {
return errorwrap.Wrapf(err, "error generating ownerrefs for service: %s", s.GetName())
}
s.SetOwnerReferences(updated)
s.SetNamespace(namespace)

// Attempt to create the Service
_, err = o.OpClient.KubernetesInterface().CoreV1().Services(plan.Namespace).Create(&s)
if k8serrors.IsAlreadyExists(err) {
// If it already exists we need to patch the existing SA with the new OwnerReferences
s.SetNamespace(plan.Namespace)
_, err = o.OpClient.UpdateService(&s)
if err != nil {
return errorwrap.Wrapf(err, "error updating service: %s", s.GetName())
}

// Mark as present
plan.Status.Plan[i].Status = v1alpha1.StepStatusPresent
} else if err != nil {
return errorwrap.Wrapf(err, "error creating service: %s", s.GetName())
} else {
// If no error occurred, mark the step as Created
plan.Status.Plan[i].Status = v1alpha1.StepStatusCreated
}

default:
return v1alpha1.ErrInvalidInstallPlan
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/registry/resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
Expand Down Expand Up @@ -113,6 +114,26 @@ func TestNamespaceResolver(t *testing.T) {
},
},
},
{
name: "SingleNewSubscription/ResolveOne/AdditionalBundleObjects/Service",
clusterState: []runtime.Object{
newSub(namespace, "a", "alpha", catalog),
},
bundlesInCatalog: []*opregistry.Bundle{
withBundleObject(bundle("b.v1", "b", "beta", "", Provides1, nil, nil, nil), u(&corev1.Service{TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: ""}, ObjectMeta: metav1.ObjectMeta{Name: "test-service"}})),
bundle("a.v1", "a", "alpha", "", nil, Requires1, nil, nil),
},
out: out{
steps: [][]*v1alpha1.Step{
bundleSteps(bundle("a.v1", "a", "alpha", "", nil, Requires1, nil, nil), namespace, catalog),
bundleSteps(withBundleObject(bundle("b.v1", "b", "beta", "", Provides1, nil, nil, nil), u(&corev1.Service{TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: ""}, ObjectMeta: metav1.ObjectMeta{Name: "test-service"}})), namespace, catalog),
subSteps(namespace, "b.v1", "b", "beta", catalog),
},
subs: []*v1alpha1.Subscription{
updatedSub(namespace, "a.v1", "a", "alpha", catalog),
},
},
},
{
name: "SingleNewSubscription/DependencyMissing",
clusterState: []runtime.Object{
Expand Down
6 changes: 6 additions & 0 deletions pkg/lib/ownerutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ func InferGroupVersionKind(obj runtime.Object) error {
}

switch obj.(type) {
case *corev1.Service:
objectKind.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Service",
})
case *corev1.ServiceAccount:
objectKind.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Expand Down

0 comments on commit faf835f

Please sign in to comment.