-
Notifications
You must be signed in to change notification settings - Fork 3
/
create.go
48 lines (41 loc) · 1.66 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cmd
import (
"os"
"k8s.io/apimachinery/pkg/types"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/kidle-dev/kidle/cmd/kidlectl/pkg"
)
// CreateCommandOptions are the options of the create command
type CreateCommandOptions struct {
Args struct {
Name string `long:"name" env:"NAME" description:"idling resource name to wakeup"`
} `positional-args:"yes" required:"1"`
Namespace string `long:"namespace" env:"NAMESPACE" short:"n" description:"IdlingResource namespace"`
Idle bool `long:"idle" env:"IDLE" short:"i" description:"the desired state of idling, defaults to false"`
Ref string `long:"ref" env:"REF" short:"r" description:"the reference to the idle-able workload"`
}
// Create executes the kidlectl create command with given args
func Create(opts CreateCommandOptions) {
// create a new kidle client
kidle, err := pkg.NewKidleClient(opts.Namespace)
if err != nil {
logf.Log.Error(err, "unable to create kidle client")
os.Exit(2)
}
logf.Log.V(0).Info("creating the idling resource", "namespace", kidle.Namespace, "name", opts.Args.Name, "ref", opts.Ref)
// create an IdlingResource
done, err := kidle.CreateIdlingResource(opts.Idle, opts.Ref, &types.NamespacedName{
Namespace: kidle.Namespace,
Name: opts.Args.Name,
})
if err != nil {
logf.Log.Error(err, "unable to create an idling resource")
os.Exit(3)
}
// handle execution result
if done {
logf.Log.V(0).Info("idling resource created", "namespace", kidle.Namespace, "name", opts.Args.Name, "ref", opts.Ref)
} else {
logf.Log.V(0).Info("creation of the idling resource failed", "namespace", kidle.Namespace, "name", opts.Args.Name, "ref", opts.Ref)
}
}