|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package scheduler |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "os" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/spf13/cobra" |
| 29 | + "sigs.k8s.io/yaml" |
| 30 | + |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/logging" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util/constants" |
| 34 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 35 | + "github.com/arangodb/kube-arangodb/pkg/util/kclient" |
| 36 | +) |
| 37 | + |
| 38 | +func InitCommand(cmd *cobra.Command) error { |
| 39 | + var c cli |
| 40 | + return c.register(cmd) |
| 41 | +} |
| 42 | + |
| 43 | +type cli struct { |
| 44 | + Namespace string |
| 45 | + |
| 46 | + Labels []string |
| 47 | + Envs []string |
| 48 | + |
| 49 | + Profiles []string |
| 50 | + |
| 51 | + Container string |
| 52 | + |
| 53 | + Image string |
| 54 | +} |
| 55 | + |
| 56 | +func (c *cli) asRequest(args ...string) (Request, error) { |
| 57 | + var r = Request{ |
| 58 | + Labels: map[string]string{}, |
| 59 | + Envs: map[string]string{}, |
| 60 | + } |
| 61 | + |
| 62 | + for _, l := range c.Labels { |
| 63 | + p := strings.SplitN(l, "=", 2) |
| 64 | + if len(p) == 1 { |
| 65 | + r.Labels[p[0]] = "" |
| 66 | + logger.Debug("Label Discovered: %s", p[0]) |
| 67 | + } else { |
| 68 | + r.Labels[p[0]] = p[1] |
| 69 | + logger.Debug("Label Discovered: %s=%s", p[0], p[1]) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for _, l := range c.Envs { |
| 74 | + p := strings.SplitN(l, "=", 2) |
| 75 | + if len(p) == 1 { |
| 76 | + return r, errors.Errorf("Missing value for env: %s", p[0]) |
| 77 | + } else { |
| 78 | + r.Envs[p[0]] = p[1] |
| 79 | + logger.Debug("Env Discovered: %s=%s", p[0], p[1]) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if len(c.Profiles) > 0 { |
| 84 | + r.Profiles = c.Profiles |
| 85 | + logger.Debug("Enabling profiles: %s", strings.Join(c.Profiles, ", ")) |
| 86 | + } |
| 87 | + |
| 88 | + r.Container = util.NewType(c.Container) |
| 89 | + if c.Image != "" { |
| 90 | + r.Image = util.NewType(c.Image) |
| 91 | + } |
| 92 | + |
| 93 | + r.Args = args |
| 94 | + |
| 95 | + return r, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (c *cli) register(cmd *cobra.Command) error { |
| 99 | + if err := logging.Init(cmd); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + cmd.RunE = c.run |
| 104 | + |
| 105 | + f := cmd.PersistentFlags() |
| 106 | + |
| 107 | + f.StringVarP(&c.Namespace, "namespace", "n", constants.NamespaceWithDefault("default"), "Kubernetes namespace") |
| 108 | + f.StringSliceVarP(&c.Labels, "label", "l", nil, "Scheduler Render Labels in format <key>=<value>") |
| 109 | + f.StringSliceVarP(&c.Envs, "env", "e", nil, "Scheduler Render Envs in format <key>=<value>") |
| 110 | + f.StringSliceVarP(&c.Profiles, "profile", "p", nil, "Scheduler Render Profiles") |
| 111 | + f.StringVar(&c.Container, "container", DefaultContainerName, "Container Name") |
| 112 | + f.StringVar(&c.Image, "image", "", "Image") |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func (c *cli) run(cmd *cobra.Command, args []string) error { |
| 118 | + if err := logging.Enable(); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + r, err := c.asRequest() |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + k, ok := kclient.GetDefaultFactory().Client() |
| 128 | + if !ok { |
| 129 | + return errors.Errorf("Unable to create Kubernetes Client") |
| 130 | + } |
| 131 | + |
| 132 | + s := NewScheduler(k, c.Namespace) |
| 133 | + |
| 134 | + rendered, profiles, err := s.Render(context.Background(), r) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + logger.Debug("Enabled profiles: %s", strings.Join(profiles, ", ")) |
| 139 | + |
| 140 | + data, err := yaml.Marshal(rendered) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + if _, err := util.WriteAll(os.Stdout, data); err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
0 commit comments