1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Add delete, export, get image-update

This uses the established abstractions to implement the usual
subcommands for the ImageUpdateAutomation type.

I've called the sub-subcommand in each case `image-update`, as a
fairly safe shorthand for the much longer `image-update-automation`.

Signed-off-by: Michael Bridgen <michael@weave.works>
This commit is contained in:
Michael Bridgen
2020-12-07 16:43:10 +00:00
parent 433628791b
commit f316aff2d3
11 changed files with 345 additions and 21 deletions

View File

@@ -94,7 +94,7 @@ func createAutoImageUpdateRun(cmd *cobra.Command, args []string) error {
},
Branch: imageUpdateArgs.branch,
},
RunInterval: &metav1.Duration{Duration: interval},
Interval: metav1.Duration{Duration: interval},
Update: autov1.UpdateStrategy{
Setters: &autov1.SettersStrategy{},
},
@@ -107,7 +107,7 @@ func createAutoImageUpdateRun(cmd *cobra.Command, args []string) error {
}
if export {
return printExport(exportImageUpdateAutomation(&update))
return printExport(exportImageUpdate(&update))
}
// I don't need these until attempting to upsert the object, but
@@ -189,21 +189,3 @@ func isImageUpdateAutomationReady(ctx context.Context, kubeClient client.Client,
return false, nil
}
}
func exportImageUpdateAutomation(update *autov1.ImageUpdateAutomation) interface{} {
gvk := autov1.GroupVersion.WithKind("ImageUpdateAutomation") // TODO replace with constant
export := autov1.ImageUpdateAutomation{
TypeMeta: metav1.TypeMeta{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: update.Name,
Namespace: update.Namespace,
Labels: update.Labels,
Annotations: update.Annotations,
},
Spec: update.Spec,
}
return export
}

View File

@@ -0,0 +1,40 @@
/*
Copyright 2020 The Flux 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 main
import (
"github.com/spf13/cobra"
autov1 "github.com/fluxcd/image-automation-controller/api/v1alpha1"
)
var deleteImageUpdateCmd = &cobra.Command{
Use: "image-update [name]",
Short: "Delete an ImageUpdateAutomation object",
Long: "The delete auto image-update command deletes the given ImageUpdateAutomation from the cluster.",
Example: ` # Delete an image update automation
flux delete auto image-update latest-images
`,
RunE: deleteCommand{
humanKind: "image update automation",
container: genericContainer{&autov1.ImageUpdateAutomation{}},
}.run,
}
func init() {
deleteAutoCmd.AddCommand(deleteImageUpdateCmd)
}

View File

@@ -0,0 +1,93 @@
/*
Copyright 2020 The Flux 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 main
import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
autov1 "github.com/fluxcd/image-automation-controller/api/v1alpha1"
)
var exportImageUpdateCmd = &cobra.Command{
Use: "image-update [name]",
Short: "Export ImageUpdateAutomation resources in YAML format",
Long: "The export image-update command exports one or all ImageUpdateAutomation resources in YAML format.",
Example: ` # Export all ImageUpdateAutomation resources
flux export auto image-update --all > updates.yaml
# Export a specific automation
flux export auto image-update latest-images > latest.yaml
`,
RunE: exportCommand{
object: exportableImageUpdate{&autov1.ImageUpdateAutomation{}},
list: exportableImageUpdateList{&autov1.ImageUpdateAutomationList{}},
}.run,
}
func init() {
exportAutoCmd.AddCommand(exportImageUpdateCmd)
}
// exportImageUpdate returns a value which has extraneous information
// stripped out.
func exportImageUpdate(item *autov1.ImageUpdateAutomation) interface{} {
gvk := autov1.GroupVersion.WithKind(autov1.ImageUpdateAutomationKind)
export := autov1.ImageUpdateAutomation{
TypeMeta: metav1.TypeMeta{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: item.Name,
Namespace: item.Namespace,
Labels: item.Labels,
Annotations: item.Annotations,
},
Spec: item.Spec,
}
return export
}
type exportableImageUpdate struct {
update *autov1.ImageUpdateAutomation
}
func (ex exportableImageUpdate) AsClientObject() runtime.Object {
return ex.update
}
func (ex exportableImageUpdate) Export() interface{} {
return exportImageUpdate(ex.update)
}
type exportableImageUpdateList struct {
list *autov1.ImageUpdateAutomationList
}
func (ex exportableImageUpdateList) AsClientObject() runtime.Object {
return ex.list
}
func (ex exportableImageUpdateList) Len() int {
return len(ex.list.Items)
}
func (ex exportableImageUpdateList) ExportAt(i int) interface{} {
return exportImageUpdate(&ex.list.Items[i])
}

View File

@@ -0,0 +1,77 @@
/*
Copyright 2020 The Flux 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 main
import (
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
autov1 "github.com/fluxcd/image-automation-controller/api/v1alpha1"
)
var getImageUpdateCmd = &cobra.Command{
Use: "image-update",
Short: "Get ImageUpdateAutomation statuses",
Long: "The get auto image-update command prints the status of ImageUpdateAutomation objects.",
Example: ` # List all image update automation object and their status
flux get auto image-update
# List image update automations from all namespaces
flux get auto image-update --all-namespaces
`,
RunE: getCommand{
list: &imageUpdateSummary{&autov1.ImageUpdateAutomationList{}},
}.run,
}
func init() {
getAutoCmd.AddCommand(getImageUpdateCmd)
}
type imageUpdateSummary struct {
*autov1.ImageUpdateAutomationList
}
func (s imageUpdateSummary) Len() int {
return len(s.Items)
}
func (s imageUpdateSummary) SummariseAt(i int, includeNamespace bool) []string {
item := s.Items[i]
status, msg := statusAndMessage(item.Status.Conditions)
var lastRun string
if item.Status.LastAutomationRunTime != nil {
lastRun = item.Status.LastAutomationRunTime.Time.Format(time.RFC3339)
}
return append(nameColumns(&item, includeNamespace), status, msg, lastRun, strings.Title(strconv.FormatBool(item.Spec.Suspend)))
}
func (s imageUpdateSummary) Headers(includeNamespace bool) []string {
headers := []string{"Name", "Ready", "Message", "Last run", "Suspended"}
if includeNamespace {
return append(namespaceHeader, headers...)
}
return headers
}
func (s imageUpdateSummary) AsClientObject() runtime.Object {
return s.ImageUpdateAutomationList
}