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 8e57adc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
13 changes: 12 additions & 1 deletion 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,11 +251,19 @@ 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,
App: appName,
InstallNamespace: packageName,
TargetNamespace: packageName + "-target",
InstallModes: installModes,
Expand Down
2 changes: 1 addition & 1 deletion internal/policy/operator/deployable_by_olm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var _ = Describe("DeployableByOLMCheck", func() {
BeforeEach(func() {
badSub := sub
Expect(deployableByOLMCheck.client.Get(testcontext, crclient.ObjectKey{
Name: "testPackage",
Name: "preflight-testPackage",
Namespace: "testPackage",
}, &badSub)).To(Succeed())
badSub.Status.InstalledCSV = ""
Expand Down
4 changes: 2 additions & 2 deletions internal/policy/operator/operator_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ var secret = corev1.Secret{

var sub = operatorsv1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: "testPackage",
Name: "preflight-testPackage",
Namespace: "testPackage",
},
Status: operatorsv1alpha1.SubscriptionStatus{
Expand All @@ -162,7 +162,7 @@ var sub = operatorsv1alpha1.Subscription{

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

0 comments on commit 8e57adc

Please sign in to comment.