|
| 1 | +/* |
| 2 | +Copyright 2020 The MayaData Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 26 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 27 | + "k8s.io/client-go/dynamic" |
| 28 | + "k8s.io/client-go/rest" |
| 29 | + "k8s.io/client-go/tools/clientcmd" |
| 30 | + "k8s.io/client-go/util/homedir" |
| 31 | + "k8s.io/client-go/util/retry" |
| 32 | + "k8s.io/klog/v2" |
| 33 | + |
| 34 | + "mayadata.io/d-operators/common/unstruct" |
| 35 | + "mayadata.io/d-operators/pkg/command" |
| 36 | + types "mayadata.io/d-operators/types/command" |
| 37 | +) |
| 38 | + |
| 39 | +var ( |
| 40 | + commandKind = flag.String( |
| 41 | + "command-kind", |
| 42 | + "Command", |
| 43 | + "Kind of Command custom resource", |
| 44 | + ) |
| 45 | + commandResource = flag.String( |
| 46 | + "command-resource", |
| 47 | + "commands", |
| 48 | + "Resource name of Command custom resource", |
| 49 | + ) |
| 50 | + commandGroup = flag.String( |
| 51 | + "command-group", |
| 52 | + "dope.metacontroller.io", |
| 53 | + "Group of Command custom resource", |
| 54 | + ) |
| 55 | + commandVersion = flag.String( |
| 56 | + "command-version", |
| 57 | + "v1", |
| 58 | + "Version of Command custom resource", |
| 59 | + ) |
| 60 | + commandName = flag.String( |
| 61 | + "command-name", |
| 62 | + "", |
| 63 | + "Name of Command custom resource", |
| 64 | + ) |
| 65 | + commandNamespace = flag.String( |
| 66 | + "command-ns", |
| 67 | + "", |
| 68 | + "Namespace of Command custom resource", |
| 69 | + ) |
| 70 | + |
| 71 | + kubeAPIServerURL = flag.String( |
| 72 | + "kube-apiserver-url", |
| 73 | + "", |
| 74 | + `Kubernetes api server url (same format as used by kubectl). |
| 75 | + If not specified, uses in-cluster config`, |
| 76 | + ) |
| 77 | + kubeconfig *string |
| 78 | + |
| 79 | + clientGoQPS = flag.Float64( |
| 80 | + "client-go-qps", |
| 81 | + 5, |
| 82 | + "Number of queries per second client-go is allowed to make (default 5)", |
| 83 | + ) |
| 84 | + clientGoBurst = flag.Int( |
| 85 | + "client-go-burst", |
| 86 | + 10, |
| 87 | + "Allowed burst queries for client-go (default 10)", |
| 88 | + ) |
| 89 | +) |
| 90 | + |
| 91 | +// main function is the entry point of this binary. |
| 92 | +// |
| 93 | +// This binary is meant to be run to completion. In other |
| 94 | +// words this does not expose any long running service. |
| 95 | +// |
| 96 | +// NOTE: |
| 97 | +// A kubernetes **Job** can make use of this binary |
| 98 | +func main() { |
| 99 | + if home := homedir.HomeDir(); home != "" { |
| 100 | + kubeconfig = flag.String( |
| 101 | + "kubeconfig", |
| 102 | + filepath.Join(home, ".kube", "config"), |
| 103 | + "(optional) absolute path to the kubeconfig file", |
| 104 | + ) |
| 105 | + } else { |
| 106 | + kubeconfig = flag.String( |
| 107 | + "kubeconfig", |
| 108 | + "", |
| 109 | + "absolute path to the kubeconfig file", |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + flag.Set("alsologtostderr", "true") |
| 114 | + flag.Parse() |
| 115 | + |
| 116 | + klogFlags := flag.NewFlagSet("klog", flag.ExitOnError) |
| 117 | + klog.InitFlags(klogFlags) |
| 118 | + |
| 119 | + // Sync the glog and klog flags. |
| 120 | + flag.CommandLine.VisitAll(func(f1 *flag.Flag) { |
| 121 | + f2 := klogFlags.Lookup(f1.Name) |
| 122 | + if f2 != nil { |
| 123 | + value := f1.Value.String() |
| 124 | + f2.Value.Set(value) |
| 125 | + } |
| 126 | + }) |
| 127 | + defer klog.Flush() |
| 128 | + |
| 129 | + if *commandName == "" { |
| 130 | + klog.Fatal("Invalid arguments: Flag 'command-name' must be set") |
| 131 | + } |
| 132 | + |
| 133 | + klog.V(1).Infof("Command custom resource: kind %s", *commandKind) |
| 134 | + klog.V(1).Infof("Command custom resource: resource %s", *commandResource) |
| 135 | + klog.V(1).Infof("Command custom resource: group %s", *commandGroup) |
| 136 | + klog.V(1).Infof("Command custom resource: version %s", *commandVersion) |
| 137 | + klog.V(1).Infof("Command custom resource: name %s", *commandName) |
| 138 | + klog.V(1).Infof("Command custom resource: namespace %s", *commandNamespace) |
| 139 | + |
| 140 | + runCommand(getRestConfig()) |
| 141 | + os.Exit(0) |
| 142 | +} |
| 143 | + |
| 144 | +func getRestConfig() *rest.Config { |
| 145 | + var config *rest.Config |
| 146 | + var err error |
| 147 | + if *kubeconfig != "" { |
| 148 | + klog.V(1).Infof("Using kubeconfig %s", *kubeconfig) |
| 149 | + config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig) |
| 150 | + } else if *kubeAPIServerURL != "" { |
| 151 | + klog.V(1).Infof("Using kubernetes api server url %s", *kubeAPIServerURL) |
| 152 | + config, err = clientcmd.BuildConfigFromFlags(*kubeAPIServerURL, "") |
| 153 | + } else { |
| 154 | + klog.V(1).Info("Using in-cluster kubeconfig") |
| 155 | + config, err = rest.InClusterConfig() |
| 156 | + } |
| 157 | + if err != nil { |
| 158 | + klog.Fatal(err) |
| 159 | + } |
| 160 | + config.QPS = float32(*clientGoQPS) |
| 161 | + config.Burst = *clientGoBurst |
| 162 | + return config |
| 163 | +} |
| 164 | + |
| 165 | +func runCommand(config *rest.Config) { |
| 166 | + client, err := dynamic.NewForConfig(config) |
| 167 | + if err != nil { |
| 168 | + klog.Fatal(err) |
| 169 | + } |
| 170 | + |
| 171 | + gvr := schema.GroupVersionResource{ |
| 172 | + Group: *commandGroup, |
| 173 | + Version: *commandVersion, |
| 174 | + Resource: *commandResource, |
| 175 | + } |
| 176 | + |
| 177 | + got, err := client.Resource(gvr). |
| 178 | + Namespace(*commandNamespace). |
| 179 | + Get( |
| 180 | + *commandName, |
| 181 | + v1.GetOptions{}, |
| 182 | + ) |
| 183 | + if err != nil { |
| 184 | + klog.Fatal(err) |
| 185 | + } |
| 186 | + |
| 187 | + var c types.Command |
| 188 | + // convert from unstructured instance to typed instance |
| 189 | + err = unstruct.ToTyped(got, &c) |
| 190 | + if err != nil { |
| 191 | + klog.Fatal(err) |
| 192 | + } |
| 193 | + |
| 194 | + cmder, err := command.NewCommander( |
| 195 | + command.CommandableConfig{ |
| 196 | + Command: &c, |
| 197 | + }, |
| 198 | + ) |
| 199 | + if err != nil { |
| 200 | + klog.Fatal(err) |
| 201 | + } |
| 202 | + status, err := cmder.Run() |
| 203 | + if err != nil { |
| 204 | + klog.Fatal(err) |
| 205 | + } |
| 206 | + |
| 207 | + retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error { |
| 208 | + // Retrieve the latest version of Command before attempting update |
| 209 | + // RetryOnConflict uses exponential backoff to avoid exhausting the apiserver |
| 210 | + got, err = client.Resource(gvr). |
| 211 | + Namespace(*commandNamespace). |
| 212 | + Get( |
| 213 | + *commandName, |
| 214 | + v1.GetOptions{}, |
| 215 | + ) |
| 216 | + if err != nil { |
| 217 | + klog.Fatal(err) |
| 218 | + } |
| 219 | + |
| 220 | + // update labels |
| 221 | + lbls := got.GetLabels() |
| 222 | + if len(lbls) == 0 { |
| 223 | + lbls = make(map[string]string) |
| 224 | + } |
| 225 | + lbls["command.dope.metacontroller.io/phase"] = string(status.Phase) |
| 226 | + got.SetLabels(lbls) |
| 227 | + |
| 228 | + // update status |
| 229 | + err = unstructured.SetNestedField( |
| 230 | + got.Object, |
| 231 | + status, |
| 232 | + "status", |
| 233 | + ) |
| 234 | + if err != nil { |
| 235 | + klog.Fatal(err) |
| 236 | + } |
| 237 | + |
| 238 | + // update command resource |
| 239 | + _, updateErr := client.Resource(gvr). |
| 240 | + Namespace(*commandNamespace). |
| 241 | + Update( |
| 242 | + got, |
| 243 | + v1.UpdateOptions{}, |
| 244 | + ) |
| 245 | + return updateErr |
| 246 | + }) |
| 247 | + if retryErr != nil { |
| 248 | + klog.Fatal(retryErr) |
| 249 | + } |
| 250 | +} |
0 commit comments