...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/clients.go

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

     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 gcp
    16  
    17  import (
    18  	"context"
    19  
    20  	"golang.org/x/oauth2/google"
    21  	"google.golang.org/api/cloudresourcemanager/v1"
    22  	"google.golang.org/api/iam/v1"
    23  	"google.golang.org/api/storage/v1"
    24  )
    25  
    26  // The user agent to track KCC's attribution to GCP usages
    27  const KCCUserAgent = "kcc/controller-manager"
    28  
    29  func NewIAMClient(ctx context.Context) (*iam.Service, error) {
    30  	httpClient, err := google.DefaultClient(ctx, iam.CloudPlatformScope)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	client, err := iam.New(httpClient)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	client.UserAgent = KCCUserAgent
    39  	return client, nil
    40  }
    41  
    42  func NewStorageClient(ctx context.Context) (*storage.Service, error) {
    43  	httpClient, err := google.DefaultClient(ctx, storage.CloudPlatformScope)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	client, err := storage.New(httpClient)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	client.UserAgent = KCCUserAgent
    52  	return client, nil
    53  }
    54  
    55  // NewCloudResourceManagerClient returns a GCP Cloud Resource Manager service.
    56  func NewCloudResourceManagerClient(ctx context.Context) (*cloudresourcemanager.Service, error) {
    57  	httpClient, err := google.DefaultClient(ctx, cloudresourcemanager.CloudPlatformScope)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	client, err := cloudresourcemanager.New(httpClient)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	client.UserAgent = KCCUserAgent
    66  	return client, nil
    67  }
    68  

View as plain text