From 958991c5b43da0cf305af0cbf812ba858b470ff8 Mon Sep 17 00:00:00 2001 From: Atanas Todorov Date: Mon, 16 Dec 2024 10:34:17 +0200 Subject: [PATCH] Initial commit --- starlark/ucp_provider.go | 96 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 starlark/ucp_provider.go diff --git a/starlark/ucp_provider.go b/starlark/ucp_provider.go new file mode 100644 index 0000000..c7cf938 --- /dev/null +++ b/starlark/ucp_provider.go @@ -0,0 +1,96 @@ +// Copyright (c) 2020 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package starlark + +import ( + "context" + "fmt" + + "github.com/pkg/errors" + "go.starlark.net/starlark" + "go.starlark.net/starlarkstruct" +) + +// UcpProviderFn is a built-in starlark function that collects Kubconfigs for all available UCP workspaces +// Starlark format: ucp_provider(kube_config=kube_config(),workspace=) +func UcpProviderFn(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + + var ( + workspace string + mgmtKubeConfig *starlarkstruct.Struct + ) + + err := starlark.UnpackArgs("ucp_provider", args, kwargs, + "mgmt_kube_config", &mgmtKubeConfig, + "workspace?", &workspace) + if err != nil { + return starlark.None, errors.Wrap(err, "failed to unpack input arguments") + } + + ctx, ok := thread.Local(identifiers.scriptCtx).(context.Context) + if !ok || ctx == nil { + return starlark.None, fmt.Errorf("script context not found") + } + + if mgmtKubeConfig == nil { + mgmtKubeConfig = thread.Local(identifiers.kubeCfg).(*starlarkstruct.Struct) + } + //mgmtKubeConfigPath, err := getKubeConfigPathFromStruct(mgmtKubeConfig) + //if err != nil { + // return starlark.None, errors.Wrap(err, "failed to extract management kubeconfig") + //} + + // if workload cluster is not supplied, then the resources for the management cluster + // should be enumerated + workspaceName := workspace + if workspaceName == "" { + //config, err := k8s.LoadKubeCfg(mgmtKubeConfigPath) + if err != nil { + return starlark.None, errors.Wrap(err, "failed to load kube config") + } + //workspaceName, err = config.GetClusterName() + //if err != nil { + // return starlark.None, errors.Wrap(err, "cannot find cluster with name "+wo) + //} + } + // + //providerConfigPath, err := provider.KubeConfig(mgmtKubeConfigPath, clusterName, namespace) + //if err != nil { + // return starlark.None, err + //} + // + //nodeAddresses, err := k8s.GetNodeAddresses(ctx, providerConfigPath, toSlice(names), toSlice(labels)) + //if err != nil { + // return starlark.None, errors.Wrap(err, "could not fetch host addresses") + //} + + // dictionary for capa provider struct + //capaProviderDict := starlark.StringDict{ + // "kind": starlark.String(identifiers.capaProvider), + // "transport": starlark.String("ssh"), + // "kube_config": starlark.String(providerConfigPath), + //} + + //// add node info to dictionary + //var nodeIps []starlark.Value + //for _, node := range nodeAddresses { + // nodeIps = append(nodeIps, starlark.String(node)) + //} + //capaProviderDict["hosts"] = starlark.NewList(nodeIps) + // + //sshConfigDict := starlark.StringDict{} + //sshConfig.ToStringDict(sshConfigDict) + // + //// modify ssh config jump credentials, if not specified + //if _, err := sshConfig.Attr("jump_host"); err != nil { + // sshConfigDict["jump_host"] = starlark.String(bastionIpAddr) + //} + //if _, err := sshConfig.Attr("jump_user"); err != nil { + // sshConfigDict["jump_user"] = starlark.String("ubuntu") + //} + //capaProviderDict[identifiers.sshCfg] = starlarkstruct.FromStringDict(starlark.String(identifiers.sshCfg), sshConfigDict) + + //return starlarkstruct.FromStringDict(starlark.String(identifiers.capaProvider), capaProviderDict), nil + return nil, nil +}