Skip to content

Commit

Permalink
KO
Browse files Browse the repository at this point in the history
  • Loading branch information
jose78 committed Dec 17, 2024
1 parent f4e5f37 commit 750a894
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 290 deletions.
7 changes: 6 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
"program": "${fileDirname}",
"args": ["deploy"],

"env": {
"KUBEALIAS": "/home/jose/ws/kubectl-fuck/resources/kube_alias.yml"
}
}
]
}
19 changes: 17 additions & 2 deletions errorManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ const (
ErrorK8sRestConfig
ErrorK8sRestResource
ErrorK8sClientConfig
ErrorK8sKubeconfgNotAccesible
ErrorJsonMarshallResourceList
ErrorKubeAliasPathNotDefined
ErrorKubeAliasReadingFile
ErrorKubeAliasVersionNotFoud
ErrorKubeAliasNotFoud
ErrorKubeAliasDuplicated
)

func (k8s errorManager) buildMsgError(params ...any) ErrorSystem {
Expand All @@ -36,10 +42,19 @@ func (k8s errorManager) buildMsgError(params ...any) ErrorSystem {
case ErrorK8sClientConfig:
errorSystem = ErrorSystem{4, fmt.Sprintf("generatig the client conf for the path %s. %v", params[0], params[1])}
case ErrorK8sRestResource:
errorSystem = ErrorSystem{5, fmt.Sprintf("getting the namespace %s;resource %v", params[1] , params[0])}
errorSystem = ErrorSystem{5, fmt.Sprintf("getting the namespace %s;resource %v", params[1], params[0])}
case ErrorJsonMarshallResourceList:
errorSystem = ErrorSystem{6, fmt.Sprintf("in the conversion of items returned of k8s to JSON for key %s", params[0])}

case ErrorK8sKubeconfgNotAccesible:
errorSystem = ErrorSystem{6, fmt.Sprintf("the kubeconfig is not accesible in this path: %s. %v", params[0], params[1])}
case ErrorKubeAliasPathNotDefined:
errorSystem = ErrorSystem{6, fmt.Sprintf("the env var KUBEALIAS is not defined")}
case ErrorKubeAliasReadingFile:
errorSystem = ErrorSystem{6, fmt.Sprintf("reading the kube_alias file in this path:%s. %v", params[0], params[1])}
case ErrorKubeAliasVersionNotFoud:
errorSystem = ErrorSystem{6, fmt.Sprintf("the version tag not found")}
case ErrorKubeAliasNotFoud:
errorSystem = ErrorSystem{6, fmt.Sprintf("the alias %s is not defined within the alias file")}
}

errorSystem.errorMsg = fmt.Sprintf("error msg: %s\nerror code: %d", errorSystem.errorMsg, errorSystem.errorCode)
Expand Down
68 changes: 46 additions & 22 deletions k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@ import (
"sync"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

const (
CTE_KUBECONFIG = "KUBECONFIG"
CTE_NS = "namespace"
CTE_TABLES = "tables"
)

func (resource defaultResource) retrieveContent(restConf *rest.Config, key string, channel chan<- map[string]string) {
dynamicClient, err := dynamic.NewForConfig(restConf)
if err != nil {
ErrorK8sGeneratingDynamicClient.buildMsgError(err).KO()
}
resourceList, err := dynamicClient.Resource(resource.GroupVersionResource).Namespace(resource.NameSpace).List(context.TODO(), metav1.ListOptions{})
if err != nil{

var resourceList *unstructured.UnstructuredList
var errResource error
if resource.NameSpace == ""{
resourceList , errResource = dynamicClient.Resource(resource.GroupVersionResource).List(context.TODO(), metav1.ListOptions{})
}else {
resourceList, errResource = dynamicClient.Resource(resource.GroupVersionResource).Namespace(resource.NameSpace).List(context.TODO(), metav1.ListOptions{})
}

if errResource != nil {
ErrorK8sRestResource.buildMsgError(resource.NameSpace, resource.GroupVersionResource).KO()
}
if conentBytes, err := json.Marshal(resourceList.Items); err != nil {
Expand All @@ -38,15 +53,28 @@ type K8sConf struct {
clientConf *kubernetes.Clientset
}

// retrieveKubeConf discover which is the path of kubeconfig
func retrieveKubeConf(ctx context.Context) string {
path := ctx.Value(CTE_KUBECONFIG)
if path != nil && path.(string) != "" {
os.Setenv(CTE_KUBECONFIG, path.(string))
}
kubeconfigPath := os.Getenv(CTE_KUBECONFIG)
if kubeconfigPath == "" {
kubeconfigPath = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
}
return kubeconfigPath
}

/*
createConfiguration given a path, generate a K8sconf to store the client and rest configuration. If the path is empty, then will verify the env var KUBECONFIG,
createConfiguration given a path, generate a K8sconf to store the client and rest configuration. If the path is empty, then will verify the env VAR_varKUBECONFIG
if is also empty then it will check the default path.
*/
func createConfiguration(path string) K8sConf {
if path != "" {
os.Setenv("KUBECONFIG", path)
os.Setenv(CTE_KUBECONFIG, path)
}
kubeconfigPath := os.Getenv("KUBECONFIG")
kubeconfigPath := os.Getenv(CTE_KUBECONFIG)
if kubeconfigPath == "" {
kubeconfigPath = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
}
Expand Down Expand Up @@ -100,33 +128,29 @@ type defaultResource struct {
NameSpace string
}

type kubeParams struct {
namespace string
k8sObjs []string
kubeconfPath string
}

func RetrieveK8sObjects(params kubeParams) map[string]string {

conf := createConfiguration(params.kubeconfPath)
mapK8sObject := generateMapObjects(conf.clientConf, params.namespace)
// retrieveK8sObjects retrieve from k8s ckuster a map of list of componentes deployed
func retrieveK8sObjects(ctx context.Context) map[string]string {
var wg sync.WaitGroup
pathK8s := retrieveKubeConf(ctx)
conf := createConfiguration(pathK8s)
ns := ctx.Value(CTE_NS).(string)
tables := ctx.Value(CTE_TABLES).([]string)
mapK8sObject := generateMapObjects(conf.clientConf, ns)

chanResult := make(chan map[string]string)
var wg sync.WaitGroup
for _, keyObject := range params.k8sObjs {
for _, keyObject := range tables {
obj, ok := mapK8sObject[keyObject]
if !ok {
ErrorK8sObjectnotSupported.buildMsgError(keyObject).KO()
}
wg.Add(1)
go obj.retrieveContent(conf.restConf, keyObject, chanResult)
go func() {
obj.retrieveContent(conf.restConf, keyObject, chanResult)
wg.Done()
}()
}

wg.Wait()
close(chanResult)

fmt.Println(mapK8sObject)

result, ok := <-chanResult
if ok {
fmt.Print("Channep open")
Expand Down
48 changes: 0 additions & 48 deletions k8s_test.go

This file was deleted.

Loading

0 comments on commit 750a894

Please sign in to comment.