...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata/gvk2stv.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metadata
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    22  
    23  	dclunstruct "github.com/GoogleCloudPlatform/declarative-resource-client-library/unstructured"
    24  	k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
    25  )
    26  
    27  // ToServiceTypeVersion returns the DCL ServiceTypeVersion given the KRM GroupVersionKind.
    28  func ToServiceTypeVersion(gvk k8sschema.GroupVersionKind, loader ServiceMetadataLoader) (dclunstruct.ServiceTypeVersion, error) {
    29  	service := groupToService(gvk.Group)
    30  	sm, found := loader.GetServiceMetadata(service)
    31  	if !found {
    32  		return dclunstruct.ServiceTypeVersion{}, fmt.Errorf("ServiceMetadata for service %v is not found", service)
    33  	}
    34  	r, found := sm.GetResourceWithKind(gvk.Kind)
    35  	if !found {
    36  		return dclunstruct.ServiceTypeVersion{}, fmt.Errorf("resource with kind %v not supported in service %v", gvk.Kind, service)
    37  	}
    38  	return dclunstruct.ServiceTypeVersion{
    39  		Service: sm.ServiceNameUsedByDCL,
    40  		Type:    r.DCLType,
    41  		Version: r.DCLVersion,
    42  	}, nil
    43  }
    44  
    45  // ToGroupVersionKind returns the KRM GroupVersionKind given the DCL ServiceTypeVersion.
    46  func ToGroupVersionKind(stv dclunstruct.ServiceTypeVersion, loader ServiceMetadataLoader) (k8sschema.GroupVersionKind, error) {
    47  	sm, found := loader.GetServiceMetadata(stv.Service)
    48  	if !found {
    49  		return k8sschema.GroupVersionKind{}, fmt.Errorf("ServiceMetadata for service %v is not found", stv.Service)
    50  	}
    51  	r, found := sm.GetResourceWithType(stv.Type)
    52  	if !found {
    53  		return k8sschema.GroupVersionKind{}, fmt.Errorf("resource with DCL type %v not supported in service %v", stv.Type, stv.Service)
    54  	}
    55  	return GVKForResource(sm, r), nil
    56  }
    57  
    58  func IsDCLBasedResourceKind(gvk k8sschema.GroupVersionKind, loader ServiceMetadataLoader) bool {
    59  	r, found := loader.GetResourceWithGVK(gvk)
    60  	if !found {
    61  		return false
    62  	}
    63  	return r.Releasable
    64  }
    65  
    66  func groupToService(group string) string {
    67  	return strings.TrimSuffix(group, k8s.ApiDomainSuffix)
    68  }
    69  

View as plain text