|
| 1 | +package cilium |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + "github.com/pulumi/pulumi/sdk/v3/go/pulumi" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | + |
| 13 | + "github.com/DataDog/test-infra-definitions/common/config" |
| 14 | + "github.com/DataDog/test-infra-definitions/common/utils" |
| 15 | + "github.com/DataDog/test-infra-definitions/components/command" |
| 16 | + "github.com/DataDog/test-infra-definitions/components/kubernetes" |
| 17 | + "github.com/DataDog/test-infra-definitions/components/remote" |
| 18 | +) |
| 19 | + |
| 20 | +//go:embed kind-cilium-cluster.yaml |
| 21 | +var kindCilumClusterFS embed.FS |
| 22 | + |
| 23 | +func kindKubeClusterConfigFromCiliumParams(params *Params) (string, error) { |
| 24 | + o := struct { |
| 25 | + KubeProxyReplacement bool |
| 26 | + }{ |
| 27 | + KubeProxyReplacement: params.hasKubeProxyReplacement(), |
| 28 | + } |
| 29 | + |
| 30 | + kindCiliumClusterTemplate, err := template.ParseFS(kindCilumClusterFS, "kind-cilium-cluster.yaml") |
| 31 | + if err != nil { |
| 32 | + return "", err |
| 33 | + } |
| 34 | + |
| 35 | + var kindCilumClusterConfig strings.Builder |
| 36 | + if err = kindCiliumClusterTemplate.Execute(&kindCilumClusterConfig, o); err != nil { |
| 37 | + return "", err |
| 38 | + } |
| 39 | + |
| 40 | + return kindCilumClusterConfig.String(), nil |
| 41 | +} |
| 42 | + |
| 43 | +func NewKindCluster(env config.Env, vm *remote.Host, name string, kubeVersion string, ciliumOpts []Option, opts ...pulumi.ResourceOption) (*kubernetes.Cluster, error) { |
| 44 | + params, err := NewParams(ciliumOpts...) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("could not create cilium params from opts: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + clusterConfig, err := kindKubeClusterConfigFromCiliumParams(params) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + cluster, err := kubernetes.NewKindClusterWithConfig(env, vm, name, kubeVersion, clusterConfig, opts...) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + |
| 59 | + if params.hasKubeProxyReplacement() { |
| 60 | + runner := vm.OS.Runner() |
| 61 | + kindClusterName := env.CommonNamer().DisplayName(49) // We can have some issues if the name is longer than 50 characters |
| 62 | + kubeConfigInternalCmd, err := runner.Command( |
| 63 | + env.CommonNamer().ResourceName("kube-kubeconfig-internal"), |
| 64 | + &command.Args{ |
| 65 | + Create: pulumi.Sprintf("kind get kubeconfig --name %s --internal", kindClusterName), |
| 66 | + }, |
| 67 | + utils.MergeOptions(opts, utils.PulumiDependsOn(cluster))..., |
| 68 | + ) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + hostPort := kubeConfigInternalCmd.StdoutOutput().ApplyT( |
| 74 | + func(v string) ([]string, error) { |
| 75 | + out := map[string]interface{}{} |
| 76 | + if err := yaml.Unmarshal([]byte(v), out); err != nil { |
| 77 | + return nil, fmt.Errorf("error unmarshaling output of kubeconfig: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + clusters := out["clusters"].([]interface{}) |
| 81 | + cluster := clusters[0].(map[string]interface{})["cluster"] |
| 82 | + server := cluster.(map[string]interface{})["server"].(string) |
| 83 | + u, err := url.Parse(server) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("could not parse server address %s: %w", server, err) |
| 86 | + } |
| 87 | + |
| 88 | + return []string{u.Hostname(), u.Port()}, nil |
| 89 | + }, |
| 90 | + ).(pulumi.StringArrayOutput) |
| 91 | + |
| 92 | + cluster.KubeInternalServerAddress = hostPort.Index(pulumi.Int(0)) |
| 93 | + cluster.KubeInternalServerPort = hostPort.Index(pulumi.Int(1)) |
| 94 | + } |
| 95 | + |
| 96 | + return cluster, nil |
| 97 | +} |
0 commit comments