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 <adc@redhat.com>
  • Loading branch information
acornett21 committed May 15, 2024
1 parent 83ee11a commit c76c6e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
17 changes: 14 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,21 @@ 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
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 = "preflight-" + 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
4 changes: 2 additions & 2 deletions internal/policy/operator/deployable_by_olm_test.go
Original file line number Diff line number Diff line change
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: "preflight-testPackage",
Namespace: "preflight-testPackage",
}, &badSub)).To(Succeed())
badSub.Status.InstalledCSV = ""
Expect(deployableByOLMCheck.client.Update(testcontext, &badSub, &crclient.UpdateOptions{})).To(Succeed())
Expand Down
8 changes: 4 additions & 4 deletions internal/policy/operator/operator_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ var secret = corev1.Secret{

var sub = operatorsv1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: "testPackage",
Namespace: "testPackage",
Name: "preflight-testPackage",
Namespace: "preflight-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: "preflight-testPackage",
Namespace: "preflight-testPackage",
},
Status: operatorsv1.OperatorGroupStatus{
LastUpdated: nil,
Expand Down

0 comments on commit c76c6e4

Please sign in to comment.