|
| 1 | +// Copyright Contributors to the Open Cluster Management project |
| 2 | +package work |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/cli-runtime/pkg/resource" |
| 13 | + workclientset "open-cluster-management.io/api/client/work/clientset/versioned" |
| 14 | + workapiv1 "open-cluster-management.io/api/work/v1" |
| 15 | +) |
| 16 | + |
| 17 | +func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { |
| 18 | + if len(args) == 0 { |
| 19 | + return fmt.Errorf("work name must be specified") |
| 20 | + } |
| 21 | + |
| 22 | + if len(args) > 1 { |
| 23 | + return fmt.Errorf("only one work name can be specified") |
| 24 | + } |
| 25 | + |
| 26 | + o.Workname = args[0] |
| 27 | + |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +func (o *Options) validate() (err error) { |
| 32 | + if len(o.Cluster) == 0 { |
| 33 | + return fmt.Errorf("the name of the cluster must be specified") |
| 34 | + } |
| 35 | + if len(*o.FileNameFlags.Filenames) == 0 { |
| 36 | + return fmt.Errorf("manifest files must be specified") |
| 37 | + } |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +func (o *Options) run() (err error) { |
| 42 | + restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig() |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + workClient, err := workclientset.NewForConfig(restConfig) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + manifests, err := o.readManifests() |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + err = o.applyWork(workClient, manifests) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + fmt.Fprintf(o.Streams.Out, "work %s in cluster %s is created\n", o.Workname, o.Cluster) |
| 62 | + return |
| 63 | +} |
| 64 | + |
| 65 | +func (o *Options) readManifests() ([]workapiv1.Manifest, error) { |
| 66 | + opt := o.FileNameFlags.ToOptions() |
| 67 | + builder := resource.NewLocalBuilder(). |
| 68 | + Unstructured(). |
| 69 | + FilenameParam(false, &opt). |
| 70 | + Flatten(). |
| 71 | + ContinueOnError() |
| 72 | + result := builder.Do() |
| 73 | + |
| 74 | + if err := result.Err(); err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + manifests := []workapiv1.Manifest{} |
| 79 | + |
| 80 | + items, err := result.Infos() |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + for _, item := range items { |
| 85 | + manifests = append(manifests, workapiv1.Manifest{RawExtension: runtime.RawExtension{Object: item.Object}}) |
| 86 | + } |
| 87 | + |
| 88 | + return manifests, nil |
| 89 | +} |
| 90 | + |
| 91 | +func (o *Options) applyWork(workClient workclientset.Interface, manifests []workapiv1.Manifest) error { |
| 92 | + work, err := workClient.WorkV1().ManifestWorks(o.Cluster).Get(context.TODO(), o.Workname, metav1.GetOptions{}) |
| 93 | + |
| 94 | + switch { |
| 95 | + case errors.IsNotFound(err): |
| 96 | + work = &workapiv1.ManifestWork{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: o.Workname, |
| 99 | + Namespace: o.Cluster, |
| 100 | + }, |
| 101 | + Spec: workapiv1.ManifestWorkSpec{ |
| 102 | + Workload: workapiv1.ManifestsTemplate{ |
| 103 | + Manifests: manifests, |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + _, createErr := workClient.WorkV1().ManifestWorks(o.Cluster).Create(context.TODO(), work, metav1.CreateOptions{}) |
| 108 | + return createErr |
| 109 | + case err != nil: |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + if !o.Overwrite { |
| 114 | + return fmt.Errorf("work %s in cluster %s already exists", o.Workname, o.Cluster) |
| 115 | + } |
| 116 | + |
| 117 | + work.Spec.Workload.Manifests = manifests |
| 118 | + _, err = workClient.WorkV1().ManifestWorks(o.Cluster).Update(context.TODO(), work, metav1.UpdateOptions{}) |
| 119 | + return err |
| 120 | +} |
0 commit comments