github.com/verrazzano/verrazzano@v1.7.0/platform-operator/namespacewatch/namespace_utils.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package namespacewatch
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"github.com/verrazzano/verrazzano/pkg/log"
    10  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    11  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    12  	"github.com/verrazzano/verrazzano/platform-operator/constants"
    13  	"github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/rancher"
    14  	"go.uber.org/zap/zapcore"
    15  	v1 "k8s.io/api/core/v1"
    16  	clipkg "sigs.k8s.io/controller-runtime/pkg/client"
    17  )
    18  
    19  // isVerrazzanoManagedNamespace checks if the given namespace is managed by Verrazzano
    20  func isVerrazzanoManagedNamespace(ns *v1.Namespace) bool {
    21  	_, verrazzanoSystemLabelExists := ns.Labels[constants.VerrazzanoManagedKey]
    22  	value, rancherSystemLabelExists := ns.Annotations[rancher.RancherSysNS]
    23  	if verrazzanoSystemLabelExists && !rancherSystemLabelExists {
    24  		return true
    25  	}
    26  	if rancherSystemLabelExists && value != "true" && verrazzanoSystemLabelExists {
    27  		return true
    28  	}
    29  	return false
    30  }
    31  
    32  // getVerrazzanoResource fetches a Verrazzano resource, if one exists
    33  func getVerrazzanoResource(client clipkg.Client) (*vzapi.Verrazzano, error) {
    34  	var err error
    35  	vzList := &vzapi.VerrazzanoList{}
    36  	if err = client.List(context.TODO(), vzList); err != nil {
    37  		return nil, err
    38  	}
    39  	if len(vzList.Items) != 1 {
    40  		return nil, fmt.Errorf("verrazzano resource list is not equal to 1")
    41  	}
    42  	return &vzList.Items[0], nil
    43  }
    44  
    45  func newLogger(vz *vzapi.Verrazzano) (vzlog.VerrazzanoLogger, error) {
    46  	zaplog, err := log.BuildZapLoggerWithLevel(2, zapcore.ErrorLevel)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	// The ID below needs to be different from the main thread, so add a suffix.
    51  	return vzlog.ForZapLogger(&vzlog.ResourceConfig{
    52  		Name:           vz.Name,
    53  		Namespace:      vz.Namespace,
    54  		ID:             string(vz.UID) + "namespacewatch",
    55  		Generation:     vz.Generation,
    56  		ControllerName: "namespacewatcher",
    57  	}, zaplog), nil
    58  }