forked from kubernetes-sigs/cluster-api-provider-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support adding ResourceManagerTags to compute instances
Signed-off-by: Carlos Salas <[email protected]>
- Loading branch information
1 parent
1fb758a
commit ce232ab
Showing
25 changed files
with
389 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
resourcemanager "cloud.google.com/go/resourcemanager/apiv3" | ||
rmpb "cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
// ResourceManagerTags defines a list of tags. | ||
type ResourceManagerTags []ResourceManagerTag | ||
|
||
// ResourceManagerTagsMap defines a map of key value pairs as expected by compute.InstanceParams.ResourceManagerTags. | ||
type ResourceManagerTagsMap map[string]string | ||
|
||
// AddResourceManagerTags binds the passed resource-manager tags to the resource. Tag keys and Tag Values | ||
// will be created by the user and only the Tag bindings to the Compute Instance will be created. | ||
// If the Tag Key/Tag Value cannot be retrieved or no tags are provided, this will be empty and no tags will be added. | ||
func AddResourceManagerTags(ctx context.Context, tagList ResourceManagerTags) ResourceManagerTagsMap { | ||
tagValueList := make(ResourceManagerTagsMap, len(tagList)) | ||
log := log.FromContext(ctx) | ||
if len(tagList) == 0 { | ||
return tagValueList | ||
} | ||
|
||
client, err := resourcemanager.NewTagValuesClient(ctx) | ||
if err != nil { | ||
log.Error(err, "failed to create tag values client") | ||
return tagValueList | ||
} | ||
|
||
getTagValuesReq := &rmpb.GetNamespacedTagValueRequest{} | ||
for _, tag := range tagList { | ||
getTagValuesReq.Name = fmt.Sprintf("%s/%s/%s", tag.ParentID, tag.Key, tag.Value) | ||
value, err := client.GetNamespacedTagValue(ctx, getTagValuesReq) | ||
if err != nil { | ||
log.Error(err, "failed to retrieve tag value") | ||
return tagValueList | ||
} | ||
tagValueList[value.Parent] = value.Name | ||
} | ||
|
||
return tagValueList | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.