Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command to list the custom packages #466

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
18 changes: 9 additions & 9 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/cmd/helpers"
"github.com/cnoe-io/idpbuilder/pkg/entity"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/kind"
"github.com/cnoe-io/idpbuilder/pkg/printer"
idpTypes "github.com/cnoe-io/idpbuilder/pkg/types"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -55,7 +55,7 @@ func list(cmd *cobra.Command, args []string) error {
}
}

func populateClusterList() ([]entity.Cluster, error) {
func populateClusterList() ([]idpTypes.Cluster, error) {
logger := helpers.CmdLogger

detectOpt, err := util.DetectKindNodeProvider()
Expand All @@ -81,7 +81,7 @@ func populateClusterList() ([]entity.Cluster, error) {
}

// Create an empty array of clusters to collect the information
clusterList := []entity.Cluster{}
clusterList := []idpTypes.Cluster{}

// List the idp builder clusters according to the provider: podman or docker
provider := cluster.NewProvider(cluster.ProviderWithLogger(kind.KindLoggerFromLogr(&logger)), detectOpt)
Expand All @@ -97,7 +97,7 @@ func populateClusterList() ([]entity.Cluster, error) {
}

for _, cluster := range clusters {
aCluster := entity.Cluster{Name: cluster}
aCluster := idpTypes.Cluster{Name: cluster}

// Search about the idp cluster within the kubeconfig file and show information
c, found := findClusterByName(config, "kind-"+cluster)
Expand Down Expand Up @@ -139,7 +139,7 @@ func populateClusterList() ([]entity.Cluster, error) {
for _, node := range nodeList.Items {
nodeName := node.Name

aNode := entity.Node{}
aNode := idpTypes.Node{}
aNode.Name = nodeName

for _, addr := range node.Status.Addresses {
Expand All @@ -158,7 +158,7 @@ func populateClusterList() ([]entity.Cluster, error) {
cpu := resources[corev1.ResourceCPU]
pods := resources[corev1.ResourcePods]

aNode.Capacity = entity.Capacity{
aNode.Capacity = idpTypes.Capacity{
Memory: float64(memory.Value()) / (1024 * 1024 * 1024),
Cpu: cpu.Value(),
Pods: pods.Value(),
Expand All @@ -181,11 +181,11 @@ func populateClusterList() ([]entity.Cluster, error) {
return clusterList, nil
}

func printAllocatedResources(ctx context.Context, k8sClient client.Client, nodeName string) (entity.Allocated, error) {
func printAllocatedResources(ctx context.Context, k8sClient client.Client, nodeName string) (idpTypes.Allocated, error) {
// List all pods on the specified node
var podList corev1.PodList
if err := k8sClient.List(ctx, &podList, client.MatchingFields{"spec.nodeName": nodeName}); err != nil {
return entity.Allocated{}, fmt.Errorf("failed to list pods on node %s.", nodeName)
return idpTypes.Allocated{}, fmt.Errorf("failed to list pods on node %s.", nodeName)
}

// Initialize counters for CPU and memory requests
Expand All @@ -204,7 +204,7 @@ func printAllocatedResources(ctx context.Context, k8sClient client.Client, nodeN
}
}

allocated := entity.Allocated{
allocated := idpTypes.Allocated{
Memory: totalMemory.String(),
Cpu: totalCPU.String(),
}
Expand Down
165 changes: 165 additions & 0 deletions pkg/cmd/get/packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package get

import (
"context"
"fmt"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/build"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/printer"
"github.com/cnoe-io/idpbuilder/pkg/types"
"github.com/spf13/cobra"
"io"
"k8s.io/client-go/util/homedir"
"os"
"path/filepath"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
)

// TODO To be removed when we will merge PR: 442 as duplicate
const (
ArgocdIngressURL = "%s://argocd.cnoe.localtest.me:%s"
)

var PackagesCmd = &cobra.Command{
Use: "packages",
Short: "retrieve packages from the cluster",
Long: ``,
RunE: getPackagesE,
SilenceUsage: true,
}

func getPackagesE(cmd *cobra.Command, args []string) error {
ctx, ctxCancel := context.WithCancel(cmd.Context())
defer ctxCancel()
kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")

opts := build.NewBuildOptions{
KubeConfigPath: kubeConfigPath,
Scheme: k8s.GetScheme(),
CancelFunc: ctxCancel,
TemplateData: v1alpha1.BuildCustomizationSpec{},
}

b := build.NewBuild(opts)

kubeConfig, err := b.GetKubeConfig()
if err != nil {
return fmt.Errorf("getting kube config: %w", err)
}

kubeClient, err := b.GetKubeClient(kubeConfig)
if err != nil {
return fmt.Errorf("getting kube client: %w", err)
}

return printPackages(ctx, os.Stdout, kubeClient, outputFormat)
}

// Print all the custom packages or based on package arguments passed using flag: -p
func printPackages(ctx context.Context, outWriter io.Writer, kubeClient client.Client, format string) error {
packageList := []types.Package{}
customPackages := v1alpha1.CustomPackageList{}
var err error

idpbuilderNamespace, err := getIDPNamespace(ctx, kubeClient)
if err != nil {
return fmt.Errorf("getting namespace: %w", err)
}

argocdBaseUrl, err := ArgocdBaseUrl(ctx, kubeClient)
if err != nil {
return fmt.Errorf("getting localbuild config: %w", err)
}

if len(packages) == 0 {
// Get all custom packages
customPackages, err = getPackages(ctx, kubeClient, idpbuilderNamespace)
if err != nil {
return fmt.Errorf("listing custom packages: %w", err)
}
} else {
// Get the custom package using its name
customPackages = v1alpha1.CustomPackageList{}
for _, name := range packages {
cp, err := getPackageByName(ctx, kubeClient, idpbuilderNamespace, name)
if err != nil {
return fmt.Errorf("getting custom package %s: %w", name, err)
}
customPackages.Items = append(customPackages.Items, cp)
}
}

for _, cp := range customPackages.Items {
newPackage := types.Package{}
newPackage.Name = cp.Name
newPackage.Namespace = cp.Namespace
newPackage.ArgocdRepository = argocdBaseUrl + "/applications/" + cp.Spec.ArgoCD.Namespace + "/" + cp.Spec.ArgoCD.Name
// There is a GitRepositoryRefs when the project has been cloned to the internal git repository
if cp.Status.GitRepositoryRefs != nil {
newPackage.GitRepository = cp.Spec.InternalGitServeURL + "/" + v1alpha1.GiteaAdminUserName + "/" + idpbuilderNamespace + "-" + cp.Status.GitRepositoryRefs[0].Name
} else {
// Default branch reference
ref := "main"
if cp.Spec.RemoteRepository.Ref != "" {
ref = cp.Spec.RemoteRepository.Ref
}
newPackage.GitRepository = cp.Spec.RemoteRepository.Url + "/tree/" + ref + "/" + cp.Spec.RemoteRepository.Path
}

newPackage.Status = strconv.FormatBool(cp.Status.Synced)

packageList = append(packageList, newPackage)
}

packagePrinter := printer.PackagePrinter{
Packages: packageList,
OutWriter: outWriter,
}
return packagePrinter.PrintOutput(format)
}

func getPackageByName(ctx context.Context, kubeClient client.Client, ns, name string) (v1alpha1.CustomPackage, error) {
p := v1alpha1.CustomPackage{}
return p, kubeClient.Get(ctx, client.ObjectKey{Name: name, Namespace: ns}, &p)
}

func getIDPConfig(ctx context.Context, kubeClient client.Client) (v1alpha1.BuildCustomizationSpec, error) {
b := v1alpha1.BuildCustomizationSpec{}
list, err := getLocalBuild(ctx, kubeClient)
if err != nil {
return b, err
}
// TODO: We assume that only one LocalBuild has been created for one cluster !
return list.Items[0].Spec.BuildCustomization, nil
}

func getIDPNamespace(ctx context.Context, kubeClient client.Client) (string, error) {
build, err := getLocalBuild(ctx, kubeClient)
if err != nil {
return "", err
}
// TODO: We assume that only one LocalBuild has been created for one cluster !
idpNamespace := v1alpha1.FieldManager + "-" + build.Items[0].Name
return idpNamespace, nil
}

func getLocalBuild(ctx context.Context, kubeClient client.Client) (v1alpha1.LocalbuildList, error) {
localBuildList := v1alpha1.LocalbuildList{}
return localBuildList, kubeClient.List(ctx, &localBuildList)
}

func getPackages(ctx context.Context, kubeClient client.Client, ns string) (v1alpha1.CustomPackageList, error) {
packageList := v1alpha1.CustomPackageList{}
return packageList, kubeClient.List(ctx, &packageList, client.InNamespace(ns))
}

// TODO To be removed when we will merge PR: 442 as duplicate
func ArgocdBaseUrl(ctx context.Context, kubeClient client.Client) (string, error) {
config, err := getIDPConfig(ctx, kubeClient)
if err != nil {
return "", err
}
return fmt.Sprintf(ArgocdIngressURL, config.Protocol, config.Port), nil
}
1 change: 1 addition & 0 deletions pkg/cmd/get/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
func init() {
GetCmd.AddCommand(ClustersCmd)
GetCmd.AddCommand(SecretsCmd)
GetCmd.AddCommand(PackagesCmd)
GetCmd.PersistentFlags().StringSliceVarP(&packages, "packages", "p", []string{}, "names of packages.")
GetCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "table", "Output format: table (default if not specified), json or yaml.")
GetCmd.PersistentFlags().StringVarP(&helpers.KubeConfigPath, "kubeconfig", "", "", "kube config file Path.")
Expand Down
12 changes: 6 additions & 6 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package get
import (
"context"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/entity"
"github.com/cnoe-io/idpbuilder/pkg/printer"
"github.com/cnoe-io/idpbuilder/pkg/types"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
Expand Down Expand Up @@ -83,7 +83,7 @@ func getSecretsE(cmd *cobra.Command, args []string) error {

func printAllPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient client.Client, format string) error {
selector := labels.NewSelector()
secrets := []entity.Secret{}
secrets := []types.Secret{}
secretPrinter := printer.SecretPrinter{
Secrets: secrets,
OutWriter: outWriter,
Expand Down Expand Up @@ -122,7 +122,7 @@ func printAllPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient

func printPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient client.Client, format string) error {
selector := labels.NewSelector()
secrets := []entity.Secret{}
secrets := []types.Secret{}
secretPrinter := printer.SecretPrinter{
OutWriter: outWriter,
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func printPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient cl
return secretPrinter.PrintOutput(format)
}

func generateSecretTable(secretTable []entity.Secret) metav1.Table {
func generateSecretTable(secretTable []types.Secret) metav1.Table {
table := &metav1.Table{}
table.ColumnDefinitions = []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
Expand Down Expand Up @@ -204,8 +204,8 @@ func generateSecretTable(secretTable []entity.Secret) metav1.Table {
return *table
}

func populateSecret(s v1.Secret, isCoreSecret bool) entity.Secret {
secret := entity.Secret{
func populateSecret(s v1.Secret, isCoreSecret bool) types.Secret {
secret := types.Secret{
Name: s.Name,
Namespace: s.Namespace,
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/get/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/entity"
"github.com/cnoe-io/idpbuilder/pkg/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestOutput(t *testing.T) {
ctx := context.Background()
r, _ := labels.NewRequirement(v1alpha1.CLISecretLabelKey, selection.Equals, []string{v1alpha1.CLISecretLabelValue})

corePkgData := map[string]entity.Secret{
corePkgData := map[string]types.Secret{
argoCDInitialAdminSecretName: {
IsCore: true,
Name: argoCDInitialAdminSecretName,
Expand All @@ -162,7 +162,7 @@ func TestOutput(t *testing.T) {
},
}

packageData := map[string]entity.Secret{
packageData := map[string]types.Secret{
"name1": {
Name: "name1",
Namespace: "ns1",
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestOutput(t *testing.T) {
assert.Nil(t, err)

// verify received json data
var received []entity.Secret
var received []types.Secret
err = json.Unmarshal(buffer.Bytes(), &received)
assert.Nil(t, err)
assert.Equal(t, 4, len(received))
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestOutput(t *testing.T) {
assert.Equal(t, 0, len(packageData))
}

func secretDataToSecret(data entity.Secret) v1.Secret {
func secretDataToSecret(data types.Secret) v1.Secret {
d := make(map[string][]byte)
if data.IsCore {
d["username"] = []byte(data.Username)
Expand Down
8 changes: 4 additions & 4 deletions pkg/printer/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package printer

import (
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/entity"
"github.com/cnoe-io/idpbuilder/pkg/types"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ClusterPrinter struct {
Clusters []entity.Cluster
Clusters []types.Cluster
OutWriter io.Writer
}

Expand All @@ -25,7 +25,7 @@ func (cp ClusterPrinter) PrintOutput(format string) error {
}
}

func generateClusterTable(input []entity.Cluster) metav1.Table {
func generateClusterTable(input []types.Cluster) metav1.Table {
table := &metav1.Table{}
table.ColumnDefinitions = []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
Expand All @@ -52,7 +52,7 @@ func generateClusterTable(input []entity.Cluster) metav1.Table {
return *table
}

func generateNodeData(nodes []entity.Node) string {
func generateNodeData(nodes []types.Node) string {
var result string
for i, aNode := range nodes {
result += aNode.Name
Expand Down
Loading
Loading