...

Source file src/edge-infra.dev/pkg/f8n/gcp/k8s/controllers/projectinit/util.go

Documentation: edge-infra.dev/pkg/f8n/gcp/k8s/controllers/projectinit

     1  package projectinit
     2  
     3  import (
     4  	"fmt"
     5  
     6  	compute "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/compute/v1beta1"
     7  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1"
     8  	resourcemgr "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/resourcemanager/v1beta1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	"edge-infra.dev/pkg/k8s/konfigkonnector/apis/meta"
    12  	"edge-infra.dev/pkg/lib/gcp/resource"
    13  )
    14  
    15  func NetworkRef(projectID string) v1alpha1.ResourceRef {
    16  	return v1alpha1.ResourceRef{
    17  		External: resource.Ref(projectID, resource.Global, "networks", "default"),
    18  	}
    19  }
    20  
    21  func objMeta(name, namespace string, project resourcemgr.Project) metav1.ObjectMeta {
    22  	return metav1.ObjectMeta{
    23  		Name:        NameWithProjectPrefix(name, project.Name),
    24  		Namespace:   namespace,
    25  		Annotations: map[string]string{meta.ProjectAnnotation: *project.Spec.ResourceID},
    26  		OwnerReferences: []metav1.OwnerReference{
    27  			*metav1.NewControllerRef(&project, resourcemgr.ProjectGVK),
    28  		},
    29  	}
    30  }
    31  
    32  func computeFirewall(meta metav1.ObjectMeta, spec compute.ComputeFirewallSpec) *compute.ComputeFirewall {
    33  	return &compute.ComputeFirewall{
    34  		TypeMeta: metav1.TypeMeta{
    35  			APIVersion: compute.SchemeGroupVersion.String(),
    36  			Kind:       compute.ComputeFirewallGVK.Kind,
    37  		},
    38  		ObjectMeta: meta,
    39  		Spec:       spec,
    40  	}
    41  }
    42  
    43  func computeSSLPolicy(meta metav1.ObjectMeta, spec compute.ComputeSSLPolicySpec) *compute.ComputeSSLPolicy {
    44  	return &compute.ComputeSSLPolicy{
    45  		TypeMeta: metav1.TypeMeta{
    46  			APIVersion: compute.SchemeGroupVersion.String(),
    47  			Kind:       compute.ComputeSSLPolicyGVK.Kind,
    48  		},
    49  		ObjectMeta: meta,
    50  		Spec:       spec,
    51  	}
    52  }
    53  
    54  // NameWithProjectPrefix ensure names are project specific to avoid conflicts when
    55  // multiple gcpprojects are in the same folder namespace. parameter project can arbirarily be a
    56  // project name or project id, it simply provides a uniform way to form a valid resource
    57  // name. trims to 63 to fit in a k8s resource name. can not guarantee uniqueness if
    58  // len(name) + len(project) > 63
    59  func NameWithProjectPrefix(name, project string) string {
    60  	newName := fmt.Sprintf("%s-%s", project, name)
    61  	if len(newName) <= 63 {
    62  		return newName
    63  	}
    64  	return newName[:63]
    65  }
    66  

View as plain text