Skip to content

Commit

Permalink
ore gcloud: add ability to label images
Browse files Browse the repository at this point in the history
Add ability to label images to facilate resource management.
  • Loading branch information
mike-nguyen committed Oct 9, 2024
1 parent 2c79278 commit 2203326
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
69 changes: 69 additions & 0 deletions mantle/cmd/ore/gcloud/label-image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2017 CoreOS, Inc.
//
// 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 gcloud

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

var (
cmdLabelImage = &cobra.Command{
Use: "label-image --name <name> --labels foo=bar ...",
Short: "label an image",
Run: runLabelImage,
}
name string
labels []string
)

func init() {
// Initialize the command and its flags
GCloud.AddCommand(cmdLabelImage)
cmdLabelImage.Flags().StringVar(&name, "name", "", "GCloud Image Name")
cmdLabelImage.Flags().StringSliceVar(&labels, "labels", []string{}, "list of key=value labels to attach to the GCloud image")
}

func runLabelImage(cmd *cobra.Command, args []string) {
if name == "" {
fmt.Fprintf(os.Stderr, "Provide --name to label\n")
os.Exit(1)
}

if len(labels) < 1 {
fmt.Fprintf(os.Stderr, "Provide at least one --label to label\n")
os.Exit(1)
}

labelMap := make(map[string]string)
for _, label := range labels {
splitLabel := strings.SplitN(label, "=", 2)
if len(splitLabel) != 2 {
fmt.Fprintf(os.Stderr, "invalid label format; should be key=value, not %v\n", label)
os.Exit(1)
}
key, value := splitLabel[0], splitLabel[1]
labelMap[key] = value
}

err := api.SetImageLabels(name, labelMap)
if err != nil {
fmt.Fprintf(os.Stderr, "couldn't create image labels: %v", err)
os.Exit(1)
}
}
22 changes: 22 additions & 0 deletions mantle/platform/api/gcloud/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,25 @@ func (a *API) SetImagePublic(name string) error {
}
return nil
}

// Add label(s) to an image
func (a *API) SetImageLabels(name string, labels map[string]string) error {

img, err := a.compute.Images.Get(a.options.Project, name).Do()
if err != nil {
return fmt.Errorf("Getting image %s failed: %v", name, err)
}

req := &compute.GlobalSetLabelsRequest{
LabelFingerprint: img.LabelFingerprint,
Labels: labels,
}

op, err := a.compute.Images.SetLabels(a.options.Project, name, req).Do()
if err != nil {
return fmt.Errorf("Adding labels to %s failed: %v", name, err)
}

plog.Infof("Updated labels on image %s: %v", name, op.Status)
return nil
}

0 comments on commit 2203326

Please sign in to comment.