-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mantle/ore: gcloud: add promote-image command #1456
Changes from all commits
98ce46b
63b23a2
f8f71c6
dc920d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2020 Red Hat | ||
// | ||
// 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 ( | ||
"github.com/coreos/mantle/platform/api/gcloud" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
var ( | ||
cmdPromoteImage = &cobra.Command{ | ||
Use: "promote-image", | ||
Short: "Promote GCP image in image family", | ||
Long: "Promote GCP image in image family and deprecate all others", | ||
Run: runPromoteImage, | ||
} | ||
|
||
promoteImageName string | ||
promoteImageFamily string | ||
) | ||
|
||
func init() { | ||
cmdPromoteImage.Flags().StringVar(&promoteImageName, "image", "", "GCP image name") | ||
cmdPromoteImage.Flags().StringVar(&promoteImageFamily, "family", "", "GCP image family to promote within") | ||
GCloud.AddCommand(cmdPromoteImage) | ||
} | ||
|
||
func deprecateImage(name string, state gcloud.DeprecationState, replacement string) { | ||
plog.Infof("Changing deprecation state of image: %v -> %v", name, state) | ||
pending, err := api.DeprecateImage(name, state, replacement) | ||
if err == nil { | ||
err = pending.Wait() | ||
} | ||
dustymabe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// New if statement to check err on api.DeprecateImage or pending.Wait() | ||
if err != nil { | ||
plog.Fatalf("Changing deprecation state of image failed: %v\n", err) | ||
} | ||
} | ||
|
||
func runPromoteImage(cmd *cobra.Command, args []string) { | ||
// Check that the user provided an image | ||
if promoteImageName == "" { | ||
plog.Fatal("Must provide an image name via --image") | ||
} | ||
bgilbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Check that the user provided an image family | ||
if promoteImageFamily == "" { | ||
plog.Fatal("Must provide an image family via --family") | ||
} | ||
|
||
plog.Infof("Attempting to promote %v in family %v", | ||
promoteImageName, promoteImageFamily) | ||
|
||
// Get all images in the image family | ||
images, err := api.ListImages(context.Background(), "", promoteImageFamily) | ||
if err != nil { | ||
plog.Fatal(err) | ||
} | ||
|
||
// Make sure the specified image exists in the specified image family | ||
found := false | ||
for _, image := range images { | ||
if image.Name == promoteImageName { | ||
found = true | ||
} | ||
} | ||
if !found { | ||
plog.Fatalf("The image (%v) must be in the image family (%v)", | ||
promoteImageName, promoteImageFamily) | ||
} | ||
|
||
// First undeprecate the image we want to promote | ||
deprecateImage(promoteImageName, gcloud.DeprecationStateActive, "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: does the If not we might need to build some annoying checks that skips an action if it's already in the correct state so that this command can be re-ran if it errors the first time through. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my experience is that it just returns successfully it it's already in the desired state, though as @bgilbert mentions in #1456 (comment) we should try to minimize the number of API calls we do by only requesting to change the deprecation state for images that need it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok updated the code so that it only calls the deprecate for images that need it |
||
|
||
// Next deprecate all other images in the image family | ||
// that need to be deprecated. | ||
for _, image := range images { | ||
// don't deprecate the image we just undeprecated | ||
if image.Name == promoteImageName { | ||
continue | ||
} | ||
// Some debug messages which are useful when needed. | ||
if image.Deprecated != nil { | ||
plog.Debugf("Deprecation state for %v is %v", | ||
image.Name, image.Deprecated.State) | ||
} else { | ||
plog.Debugf("Deprecation state is nil for %v", image.Name) | ||
} | ||
// Perform the deprecation if the image is not already deprecated. | ||
// We detect if it is active by checking if it either doesn't | ||
// have any deprecation state or if it is explicitly ACTIVE. | ||
if image.Deprecated == nil || | ||
image.Deprecated.State == string(gcloud.DeprecationStateActive) { | ||
deprecateImage( | ||
image.Name, | ||
gcloud.DeprecationStateDeprecated, | ||
promoteImageName, | ||
) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In principle we should collect up all the pendings and wait on them at the end. I don't know how much it matters here, though, since we're probably only deprecating one image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right this should be one API call IIUC