generated from diginfra/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_in_cluster.go
94 lines (85 loc) · 3.07 KB
/
kubernetes_in_cluster.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2023 The Diginfra 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 cmd
import (
"bytes"
"github.com/diginfra/driverkit/pkg/driverbuilder"
"github.com/diginfra/driverkit/pkg/driverbuilder/builder"
"github.com/diginfra/driverkit/pkg/kubernetes/factory"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// NewKubernetesInClusterCmd creates the `driverkit kubernetes` command.
func NewKubernetesInClusterCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *pflag.FlagSet) *cobra.Command {
kubernetesInClusterCmd := &cobra.Command{
Use: "kubernetes-in-cluster",
Short: "Build Diginfra kernel modules and eBPF probes against a Kubernetes cluster inside a Kubernetes cluster.",
Aliases: []string{"k8s-ic"},
}
// Add Kubernetes pods options flags
flags := kubernetesInClusterCmd.Flags()
addKubernetesFlags(flags)
kubernetesInClusterCmd.PersistentFlags().AddFlagSet(flags)
// Add root flags
kubernetesInClusterCmd.PersistentFlags().AddFlagSet(rootFlags)
kubernetesInClusterCmd.RunE = func(c *cobra.Command, args []string) error {
configOpts.Printer.Logger.Info("starting build",
configOpts.Printer.Logger.Args("processor", c.Name()))
if !configOpts.dryRun {
if !rootOpts.Output.HasOutputs() {
configOpts.Printer.Logger.Info("no output specified")
return nil
}
// Since we use a spinner, cache log data to a bytesbuffer;
// we will later print it once we stop the spinner.
var b *builder.Build
if configOpts.disableStyling {
b = rootOpts.ToBuild(configOpts.Printer)
} else {
var buf bytes.Buffer
b = rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
}
return kubernetesInClusterRun(b, configOpts)
}
return nil
}
return kubernetesInClusterCmd
}
func kubernetesInClusterRun(b *builder.Build, configOpts *ConfigOptions) error {
kubeConfig, err := rest.InClusterConfig()
if err != nil {
return err
}
if err = factory.SetKubernetesDefaults(kubeConfig); err != nil {
return err
}
kc, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return err
}
buildProcessor := driverbuilder.NewKubernetesBuildProcessor(kc.CoreV1(),
kubeConfig,
kubernetesOptions.RunAsUser,
kubernetesOptions.Namespace,
kubernetesOptions.ImagePullSecret,
configOpts.Timeout,
configOpts.ProxyURL)
return buildProcessor.Start(b)
}