Skip to content

Commit

Permalink
adding logic to deployablebyolm to validate that packagename complies…
Browse files Browse the repository at this point in the history
… with DNS-1035 labeling, ensuring that CatalogSources can be created

Signed-off-by: Adam D. Cornett <[email protected]>
  • Loading branch information
acornett21 authored and bcrochet committed May 20, 2024
1 parent 83ee11a commit 354d10d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
18 changes: 15 additions & 3 deletions internal/policy/operator/deployable_by_olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
apiruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation"
ctrl "sigs.k8s.io/controller-runtime"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -214,6 +215,8 @@ func checkImageSource(ctx context.Context, operatorImages []string) bool {
}

func (p *DeployableByOlmCheck) operatorMetadata(ctx context.Context, bundleRef image.ImageReference) (*operatorData, error) {
logger := logr.FromContextOrDiscard(ctx)

// retrieve the operator metadata from bundle image
annotationsFileName := filepath.Join(bundleRef.ImageFSPath, "metadata", "annotations.yaml")
annotationsFile, err := os.Open(annotationsFileName)
Expand Down Expand Up @@ -248,13 +251,22 @@ func (p *DeployableByOlmCheck) operatorMetadata(ctx context.Context, bundleRef i
installModes[val.Type] = val
}

// validating that package name complies with DNS-1035 labeling constraints
// ensuring that CatalogSources can be created/referenced in cluster and reconciles properly
// if there is an error we need to prefix the name so the install/reconciles can continue
appName := packageName
if msgs := validation.IsDNS1035Label(packageName); len(msgs) != 0 {
logger.V(log.DBG).Info(fmt.Sprintf("package name %s does not comply with DNS-1035, prefixing to avoid errors", packageName))
appName = "p-" + packageName
}

return &operatorData{
CatalogImage: catalogImage,
Channel: channel,
PackageName: packageName,
App: packageName,
InstallNamespace: packageName,
TargetNamespace: packageName + "-target",
App: appName,
InstallNamespace: appName,
TargetNamespace: appName + "-target",
InstallModes: installModes,
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/policy/operator/deployable_by_olm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var _ = Describe("DeployableByOLMCheck", func() {

// changing the namespace since OwnNamespace operators CSV get applied to `InstallNamespace`
ownCSV := csv.DeepCopy()
ownCSV.Namespace = "testPackage"
ownCSV.Namespace = "p-testPackage"

deployableByOLMCheck.client = clientBuilder.
WithObjects(ownCSV).
Expand Down Expand Up @@ -108,8 +108,8 @@ var _ = Describe("DeployableByOLMCheck", func() {
BeforeEach(func() {
badSub := sub
Expect(deployableByOLMCheck.client.Get(testcontext, crclient.ObjectKey{
Name: "testPackage",
Namespace: "testPackage",
Name: "p-testPackage",
Namespace: "p-testPackage",
}, &badSub)).To(Succeed())
badSub.Status.InstalledCSV = ""
Expect(deployableByOLMCheck.client.Update(testcontext, &badSub, &crclient.UpdateOptions{})).To(Succeed())
Expand Down
10 changes: 5 additions & 5 deletions internal/policy/operator/operator_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var pods = corev1.PodList{
var csv = operatorsv1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "csv-v0.0.0",
Namespace: "testPackage-target",
Namespace: "p-testPackage-target",
},
Spec: operatorsv1alpha1.ClusterServiceVersionSpec{},
Status: operatorsv1alpha1.ClusterServiceVersionStatus{
Expand Down Expand Up @@ -152,8 +152,8 @@ var secret = corev1.Secret{

var sub = operatorsv1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: "testPackage",
Namespace: "testPackage",
Name: "p-testPackage",
Namespace: "p-testPackage",
},
Status: operatorsv1alpha1.SubscriptionStatus{
InstalledCSV: "csv-v0.0.0",
Expand All @@ -162,8 +162,8 @@ var sub = operatorsv1alpha1.Subscription{

var og = operatorsv1.OperatorGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "testPackage",
Namespace: "testPackage",
Name: "p-testPackage",
Namespace: "p-testPackage",
},
Status: operatorsv1.OperatorGroupStatus{
LastUpdated: nil,
Expand Down

0 comments on commit 354d10d

Please sign in to comment.