Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
beneiltis committed Oct 16, 2023
2 parents ffa743d + b85f279 commit 874353b
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 90 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
branches:
- develop
# repository_dispatch:
# types: [build-from-punq-frontend-dev]

env:
IMAGE_NAME: ghcr.io/mogenius/punq-dev
Expand Down Expand Up @@ -43,7 +41,7 @@ jobs:
- name: download and inject punq-frontend
run: |
curl https://github.com/mogenius/punq-frontend/releases/download/latest/latest.tar.gz -L -o ui.tar.gz
curl https://github.com/mogenius/punq-frontend/releases/download/latest/latest-dev.tar.gz -L -o ui.tar.gz
mkdir -p ui/dist
tar -xzf ui.tar.gz -C ui/dist
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
branches:
- main
# repository_dispatch:
# types: [build-from-punq-frontend]

env:
IMAGE_NAME: ghcr.io/mogenius/punq
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ tarballs: all
tar -czvf builds/`basename "$$file"`.tar.gz -C builds `basename "$$file"`; \
done

update-frontend:
@curl https://github.com/mogenius/punq-frontend/releases/download/latest/latest.tar.gz -L -o ui.tar.gz
@mkdir -p ui/dist
@tar -xzf ui.tar.gz -C ui/dist
@rm ui.tar.gz

clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)-$(VERSION)-darwin-amd64
Expand Down
18 changes: 18 additions & 0 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/mogenius/punq/dtos"
"github.com/mogenius/punq/kubernetes"
"github.com/mogenius/punq/services"
"github.com/mogenius/punq/structs"
"github.com/mogenius/punq/utils"
Expand Down Expand Up @@ -43,6 +45,22 @@ var addContextCmd = &cobra.Command{
if err != nil {
utils.FatalError(err.Error())
}

// Test Clusters for Reachability
for i := 0; i < len(contexts); i++ {
fmt.Printf("[%02d/%d] Testing context '%s'...", i+1, len(contexts), contexts[i].Name)
testResult, provider, err := kubernetes.CheckContext(contexts[i])
contexts[i].Reachable = testResult
contexts[i].Provider = string(provider)
if err != nil {
elements := strings.Split(err.Error(), ":")
if len(elements) > 0 {
fmt.Printf(" (%s) ", elements[len(elements)-1])
}
}
fmt.Printf(" %s\n", utils.StatusEmoji(testResult))
}

dtos.ListContextsToTerminal(contexts)

index := utils.SelectIndexInteractive("Select context to add", len(contexts))
Expand Down
23 changes: 8 additions & 15 deletions cmd/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,50 +55,50 @@ var checkCmd = &cobra.Command{
// check internet access
inetResult, inetErr := utils.CheckInternetAccess()
t.AppendRow(
table.Row{"Internet Access", StatusEmoji(inetResult), StatusMessage(inetErr, "Check your internet connection.", "Internet access works.")},
table.Row{"Internet Access", utils.StatusEmoji(inetResult), StatusMessage(inetErr, "Check your internet connection.", "Internet access works.")},
)
t.AppendSeparator()

// check for kubectl
kubectlResult, kubectlOutput, kubectlErr := utils.IsKubectlInstalled()
t.AppendRow(
table.Row{"kubectl", StatusEmoji(kubectlResult), StatusMessage(kubectlErr, "Plase install kubectl (https://kubernetes.io/docs/tasks/tools/) on your system to proceed.", kubectlOutput)},
table.Row{"kubectl", utils.StatusEmoji(kubectlResult), StatusMessage(kubectlErr, "Plase install kubectl (https://kubernetes.io/docs/tasks/tools/) on your system to proceed.", kubectlOutput)},
)
t.AppendSeparator()

// check kubernetes version
kubernetesVersion := kubernetes.KubernetesVersion(nil)
kubernetesVersionResult := kubernetesVersion != nil
t.AppendRow(
table.Row{"Kubernetes Version", StatusEmoji(kubernetesVersionResult), StatusMessage(kubectlErr, "Cannot determin version of kubernetes.", fmt.Sprintf("Version: %s\nPlatform: %s", kubernetesVersion.String(), kubernetesVersion.Platform))},
table.Row{"Kubernetes Version", utils.StatusEmoji(kubernetesVersionResult), StatusMessage(kubectlErr, "Cannot determin version of kubernetes.", fmt.Sprintf("Version: %s\nPlatform: %s", kubernetesVersion.String(), kubernetesVersion.Platform))},
)
t.AppendSeparator()

// check for ingresscontroller
ingressType, ingressTypeErr := kubernetes.DetermineIngressControllerType(nil)
t.AppendRow(
table.Row{"Ingress Controller", StatusEmoji(ingressTypeErr == nil), StatusMessage(ingressTypeErr, "Cannot determin ingress controller type.", ingressType.String())},
table.Row{"Ingress Controller", utils.StatusEmoji(ingressTypeErr == nil), StatusMessage(ingressTypeErr, "Cannot determin ingress controller type.", ingressType.String())},
)
t.AppendSeparator()

// check for metrics server
metricsResult, metricsVersion, metricsErr := kubernetes.IsMetricsServerAvailable(nil)
t.AppendRow(
table.Row{"Metrics Server", StatusEmoji(metricsResult), StatusMessage(metricsErr, "kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml\nNote: Running docker-desktop? Please add '- --kubelet-insecure-tls' to the args sction in the deployment of metrics-server.", metricsVersion)},
table.Row{"Metrics Server", utils.StatusEmoji(metricsResult), StatusMessage(metricsErr, "kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml\nNote: Running docker-desktop? Please add '- --kubelet-insecure-tls' to the args sction in the deployment of metrics-server.", metricsVersion)},
)
t.AppendSeparator()

// check for helm
helmResult, helmOutput, helmErr := utils.IsHelmInstalled()
t.AppendRow(
table.Row{"Helm", StatusEmoji(helmResult), StatusMessage(helmErr, "Plase install helm (https://helm.sh/docs/intro/install/) on your system to proceed.", helmOutput)},
table.Row{"Helm", utils.StatusEmoji(helmResult), StatusMessage(helmErr, "Plase install helm (https://helm.sh/docs/intro/install/) on your system to proceed.", helmOutput)},
)
t.AppendSeparator()

// check cluster provider
clusterProvOutput, clusterProvErr := kubernetes.GuessClusterProvider(nil)
t.AppendRow(
table.Row{"Cluster Provider", StatusEmoji(clusterProvErr == nil), StatusMessage(clusterProvErr, "We could not determine the provider of this cluster.", string(clusterProvOutput))},
table.Row{"Cluster Provider", utils.StatusEmoji(clusterProvErr == nil), StatusMessage(clusterProvErr, "We could not determine the provider of this cluster.", string(clusterProvOutput))},
)
t.AppendSeparator()

Expand All @@ -110,7 +110,7 @@ var checkCmd = &cobra.Command{
}
apiVersStr = strings.TrimRight(apiVersStr, "\n\r")
t.AppendRow(
table.Row{"API Versions", StatusEmoji(len(apiVerResult) > 0), StatusMessage(apiVerErr, "Cannot determin API versions.", apiVersStr)},
table.Row{"API Versions", utils.StatusEmoji(len(apiVerResult) > 0), StatusMessage(apiVerErr, "Cannot determin API versions.", apiVersStr)},
)
t.Render()
},
Expand Down Expand Up @@ -148,13 +148,6 @@ func init() {

// UTILS

func StatusEmoji(works bool) string {
if works {
return "✅"
}
return "❌"
}

func StatusMessage(err error, solution string, successMsg string) string {
if err != nil {
return fmt.Sprintf("Error: %s\nSolution: %s", err.Error(), solution)
Expand Down
1 change: 1 addition & 0 deletions dtos/kubernetes-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dtos
type KubernetesProvider string

const (
UNKNOWN KubernetesProvider = "UNKNOWN"
BRING_YOUR_OWN KubernetesProvider = "BRING_YOUR_OWN"
DOCKER_ENTERPRISE KubernetesProvider = "DOCKER_ENTERPRISE" // Docker
DOCKER_DESKTOP KubernetesProvider = "DOCKER_DESKTOP" // Docker
Expand Down
5 changes: 3 additions & 2 deletions dtos/punq-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type PunqContext struct {
ContextHash string `json:"contextHash" validate:"required"`
Context string `json:"context" validate:"required"`
Provider string `json:"provider" validate:"required"`
Reachable bool `json:"reachable" validate:"required"`
Access []PunqAccess `json:"access" validate:"required"`
}

Expand Down Expand Up @@ -96,7 +97,7 @@ func (c *PunqContext) PrintToTerminal() {
func ListContextsToTerminal(contexts []PunqContext) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "ID", "Name", "Access", "Hash"})
t.AppendHeader(table.Row{"#", "ID", "Name", "Reachable", "Provider", "Access"})
for index, context := range contexts {
accessStr := "*"
accessEntries := []string{}
Expand All @@ -107,7 +108,7 @@ func ListContextsToTerminal(contexts []PunqContext) {
accessStr = strings.Join(accessEntries, ", ")
}
t.AppendRow(
table.Row{index + 1, context.Id, context.Name, accessStr, context.ContextHash},
table.Row{index + 1, context.Id, context.Name, utils.StatusEmoji(context.Reachable), context.Provider, accessStr},
)
}
t.Render()
Expand Down
63 changes: 9 additions & 54 deletions kubernetes/addResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,60 +225,6 @@ func applyNamespace(provider *KubeProvider) {
fmt.Println("Created punq namespace. ✅")
}

// func CreateClusterSecretIfNotExist(provider *KubeProvider) (utils.ClusterSecret, error) {
// secretClient := provider.ClientSet.CoreV1().Secrets(utils.CONFIG.Kubernetes.OwnNamespace)

// existingSecret, getErr := secretClient.Get(context.TODO(), utils.CONFIG.Kubernetes.OwnNamespace, metav1.GetOptions{})
// return writePunqSecret(secretClient, existingSecret, getErr)
// }

// func writePunqSecret(secretClient v1.SecretInterface, existingSecret *core.Secret, getErr error) (utils.ClusterSecret, error) {
// clusterSecret := utils.ClusterSecret{
// ApiKey: utils.NanoIdExtraLong(),
// ClusterMfaId: utils.NanoIdExtraLong(),
// ClusterName: utils.CONFIG.Kubernetes.ClusterName,
// }

// secret := utils.InitSecret()
// secret.ObjectMeta.Name = utils.CONFIG.Kubernetes.OwnNamespace
// secret.ObjectMeta.Namespace = utils.CONFIG.Kubernetes.OwnNamespace
// delete(secret.StringData, "exampleData") // delete example data
// secret.StringData["cluster-mfa-id"] = clusterSecret.ClusterMfaId
// secret.StringData["api-key"] = clusterSecret.ApiKey
// secret.StringData["cluster-name"] = clusterSecret.ClusterName

// if existingSecret == nil || getErr != nil {
// logger.Log.Info("Creating new punq secret ...")
// _, err := secretClient.Create(context.TODO(), &secret, MoCreateOptions())
// if err != nil {
// logger.Log.Error(err)
// return clusterSecret, err
// }
// fmt.Println("Created new punq secret. ✅")
// } else {
// if string(existingSecret.Data["api-key"]) != clusterSecret.ApiKey ||
// string(existingSecret.Data["cluster-name"]) != clusterSecret.ClusterName {
// fmt.Println("Updating existing punq secret ...")
// // keep existing mfa-id if possible
// if string(existingSecret.Data["cluster-mfa-id"]) != "" {
// clusterSecret.ClusterMfaId = string(existingSecret.Data["cluster-mfa-id"])
// secret.StringData["cluster-mfa-id"] = clusterSecret.ClusterMfaId
// }
// _, err := secretClient.Update(context.TODO(), &secret, MoUpdateOptions())
// if err != nil {
// logger.Log.Error(err)
// return clusterSecret, err
// }
// fmt.Println("Updated punq secret. ✅")
// } else {
// clusterSecret.ClusterMfaId = string(existingSecret.Data["cluster-mfa-id"])
// fmt.Println("Using existing punq secret. ✅")
// }
// }

// return clusterSecret, nil
// }

func CreateContextSecretIfNotExist(provider *KubeProvider) (*dtos.PunqContext, error) {
secretClient := provider.ClientSet.CoreV1().Secrets(utils.CONFIG.Kubernetes.OwnNamespace)

Expand All @@ -299,6 +245,15 @@ func writeContextSecret(secretClient v1.SecretInterface, existingSecret *core.Se
utils.FatalError(err.Error())
}

fmt.Println("Determining cluster provider ...")
ownProvider, err := GuessClusterProvider(nil)
if err == nil {
ownContext.Provider = string(ownProvider)
fmt.Printf("Determined cluster provider: '%s'. ✅\n", ownProvider)
} else {
fmt.Println("Determining cluster provider failed.")
}

ownContext.Id = utils.CONTEXTOWN
ownContext.Name = utils.CONTEXTOWN

Expand Down
33 changes: 33 additions & 0 deletions kubernetes/context.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package kubernetes

import (
"context"
"errors"
"fmt"
"os"

"github.com/mogenius/punq/dtos"
"github.com/mogenius/punq/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

var allContexts []dtos.PunqContext = []dtos.PunqContext{}
Expand Down Expand Up @@ -63,3 +67,32 @@ func contextWrite(id string) error {
}
return nil
}

func CheckContext(ctx dtos.PunqContext) (bool, dtos.KubernetesProvider, error) {
configFromString, err := clientcmd.NewClientConfigFromBytes([]byte(ctx.Context))
if err != nil {
return false, dtos.UNKNOWN, err
}

config, err := configFromString.ClientConfig()
if err != nil {
return false, dtos.UNKNOWN, err
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return false, dtos.UNKNOWN, err
}

nodeList, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, dtos.UNKNOWN, err
}

provider, err := GuessCluserProviderFromNodeList(nodeList)
if err != nil {
return false, dtos.UNKNOWN, err
} else {
return true, provider, nil
}
}
23 changes: 17 additions & 6 deletions kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,15 @@ func GuessClusterProvider(contextId *string) (dtos.KubernetesProvider, error) {
return dtos.SELF_HOSTED, err
}

return GuessCluserProviderFromNodeList(nodes)
}

func GuessCluserProviderFromNodeList(nodes *v1.NodeList) (dtos.KubernetesProvider, error) {

for _, node := range nodes.Items {
labels := node.GetLabels()

if LabelsContain(labels, "eks.amazonaws.com/capacity") {
if LabelsContain(labels, "eks.amazonaws.com/") {
return dtos.EKS, nil
} else if LabelsContain(labels, "docker-desktop") {
return dtos.DOCKER_DESKTOP, nil
Expand Down Expand Up @@ -678,23 +683,29 @@ func GuessClusterProvider(contextId *string) (dtos.KubernetesProvider, error) {
return dtos.GKE_ON_PREM, nil
} else if LabelsContain(labels, "rke.cattle.io") {
return dtos.RKE, nil
} else {
fmt.Println("This cluster's provider is unknown or it might be self-managed.")
return dtos.UNKNOWN, nil
}
}

fmt.Println("This cluster's provider is unknown or it might be self-managed.")
return dtos.SELF_HOSTED, nil
return dtos.UNKNOWN, nil
}

func LabelsContain(labels map[string]string, str string) bool {
// Keys
// Keys EQUAL
if _, ok := labels[strings.ToLower(str)]; ok {
return true
}

// Values
for _, label := range labels {
for key, label := range labels {
if strings.EqualFold(label, str) {
return true
}
// KEY CONTAINS
if strings.Contains(key, str) {
return true
}
}
return false
}
Expand Down
Loading

0 comments on commit 874353b

Please sign in to comment.