...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/logging_logsink.go

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

     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 resourceoverrides
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/operations"
    23  
    24  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  )
    26  
    27  func GetLoggingLogSinkResourceOverrides() ResourceOverrides {
    28  	ro := ResourceOverrides{
    29  		Kind: "LoggingLogSink",
    30  	}
    31  	ro.Overrides = append(ro.Overrides, buildLoggingLogSink())
    32  	return ro
    33  }
    34  
    35  type LoggingLogSink struct {
    36  }
    37  
    38  func buildLoggingLogSink() ResourceOverride {
    39  	h := &LoggingLogSink{}
    40  
    41  	o := ResourceOverride{
    42  		PreTerraformExport: h.PreTerraformExport,
    43  		CRDDecorate:        h.CRDDecorate,
    44  	}
    45  
    46  	return o
    47  }
    48  
    49  func (h *LoggingLogSink) CRDDecorate(crd *apiextensions.CustomResourceDefinition) error {
    50  	// Add description to mention only external field is supported for loggingLogBucketRef.
    51  	// See b/221957221 for context.
    52  	// TODO(b/227524735): Remove this ResourceOverride when b/200585845 is implemented
    53  	schema := k8s.GetOpenAPIV3SchemaFromCRD(crd)
    54  	spec := schema.Properties["spec"]
    55  	destination := spec.Properties["destination"]
    56  	loggingLogBucketRef := destination.Properties["loggingLogBucketRef"]
    57  	loggingLogBucketRef.Description = "Only `external` field is supported to configure the reference."
    58  	destination.Properties["loggingLogBucketRef"] = loggingLogBucketRef
    59  	return nil
    60  }
    61  
    62  func (h *LoggingLogSink) PreTerraformExport(ctx context.Context, op *operations.TerraformExport) error {
    63  	// google_logging_log_sink is a KCC-only terraform resource,
    64  	// so map back to google_logging_project_sink / google_logging_folder_sink / google_logging_organization_sink
    65  
    66  	projectID := op.TerraformState.Attributes["project"]
    67  	orgID := op.TerraformState.Attributes["org_id"]
    68  	folderID := op.TerraformState.Attributes["folder"]
    69  
    70  	if projectID != "" && orgID != "" {
    71  		return fmt.Errorf("project=%q and org_id=%q were both set", projectID, orgID)
    72  	}
    73  	if projectID != "" && folderID != "" {
    74  		return fmt.Errorf("project=%q and folder=%q were both set", projectID, folderID)
    75  	}
    76  	if orgID != "" && folderID != "" {
    77  		return fmt.Errorf("org_id=%q and folder=%q were both set", orgID, folderID)
    78  	}
    79  
    80  	if projectID != "" {
    81  		op.TerraformInfo.Type = "google_logging_project_sink"
    82  	} else if folderID != "" {
    83  		op.TerraformInfo.Type = "google_logging_folder_sink"
    84  	} else if orgID != "" {
    85  		op.TerraformInfo.Type = "google_logging_organization_sink"
    86  	} else {
    87  		return fmt.Errorf("unable to determine whether LoggingLogSink is org or project or folder scoped")
    88  	}
    89  
    90  	return nil
    91  }
    92  

View as plain text