Skip to content

Commit

Permalink
Add flag --installed to command modules (#2130)
Browse files Browse the repository at this point in the history
Co-authored-by: MichalKalke <michalkalke@gmail.com>
  • Loading branch information
Cortey and MichalKalke authored Jun 11, 2024
1 parent ca0988a commit dc07ebe
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 10 deletions.
76 changes: 66 additions & 10 deletions internal/cmd/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/kyma-project/cli.v3/internal/model"
"io"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -24,6 +26,8 @@ type modulesConfig struct {
installed bool
}

const URL = "https://raw.githubusercontent.com/kyma-project/community-modules/main/model.json"

func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cfg := modulesConfig{
KymaConfig: kymaConfig,
Expand Down Expand Up @@ -57,6 +61,7 @@ func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {

func runModules(cfg *modulesConfig) clierror.Error {
var err error

if cfg.catalog {
modules, err := listAllModules()
if err != nil {
Expand All @@ -81,24 +86,42 @@ func runModules(cfg *modulesConfig) clierror.Error {
}

if cfg.installed {
clierror.Wrap(err, clierror.New("not implemented yet, please use the catalog or managed flag"))
installed, err := listInstalledModules(cfg)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to list installed Kyma modules"))
}
fmt.Println("Installed modules:\n")
for _, rec := range installed {
fmt.Println(rec)
}
return nil
}

return clierror.Wrap(err, clierror.New("failed to get modules", "please use one of: catalog, managed or installed flags"))
}

func listAllModules() ([]string, clierror.Error) {
resp, err := http.Get("https://raw.githubusercontent.com/kyma-project/community-modules/main/model.json")
resp, err := http.Get(URL)
if err != nil {
return nil, clierror.Wrap(err, clierror.New("while getting modules list from github"))
}
defer resp.Body.Close()

var template []struct {
Name string `json:"name"`
var template model.Module

template, respErr := handleResponse(err, resp, template)
if respErr != nil {
return nil, clierror.WrapE(respErr, clierror.New("while handling response"))
}

var out []string
for _, rec := range template {
out = append(out, rec.Name)
}
return out, nil
}

func handleResponse(err error, resp *http.Response, template model.Module) (model.Module, clierror.Error) {
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
return nil, clierror.Wrap(err, clierror.New("while reading http response"))
Expand All @@ -107,12 +130,7 @@ func listAllModules() ([]string, clierror.Error) {
if err != nil {
return nil, clierror.Wrap(err, clierror.New("while unmarshalling"))
}

var out []string
for _, rec := range template {
out = append(out, rec.Name)
}
return out, nil
return template, nil
}

func listManagedModules(cfg *modulesConfig) ([]string, clierror.Error) {
Expand All @@ -135,6 +153,44 @@ func listManagedModules(cfg *modulesConfig) ([]string, clierror.Error) {
return moduleNames, nil
}

func listInstalledModules(cfg *modulesConfig) ([]string, clierror.Error) {
resp, err := http.Get(URL)
if err != nil {
return nil, clierror.Wrap(err, clierror.New("while getting modules list from github"))
}
defer resp.Body.Close()

var template model.Module

template, respErr := handleResponse(err, resp, template)
if respErr != nil {
return nil, clierror.WrapE(respErr, clierror.New("while handling response"))
}

var out []string
for _, rec := range template {
managerPath := strings.Split(rec.Versions[0].ManagerPath, "/")
managerName := managerPath[len(managerPath)-1]
version := rec.Versions[0].Version
deployment, err := cfg.KubeClient.Static().AppsV1().Deployments("kyma-system").Get(cfg.Ctx, managerName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
msg := "while getting the " + managerName + " deployment"
return nil, clierror.Wrap(err, clierror.New(msg))
}
if !errors.IsNotFound(err) {
deploymentImage := strings.Split(deployment.Spec.Template.Spec.Containers[0].Image, "/")
installedVersion := strings.Split(deploymentImage[len(deploymentImage)-1], ":")

if version == installedVersion[len(installedVersion)-1] {
out = append(out, rec.Name+" - "+installedVersion[len(installedVersion)-1])
} else {
out = append(out, rec.Name+" - "+"outdated version, latest version is "+version)
}
}
}
return out, nil
}

func getModuleNames(unstruct *unstructured.Unstructured) ([]string, error) {
var moduleNames []string
managedFields := unstruct.GetManagedFields()
Expand Down
37 changes: 37 additions & 0 deletions internal/model/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package model

type Module []struct {
Name string `json:"name,omitempty"`
Manageable bool `json:"manageable,omitempty"`
Versions []Versions `json:"versions,omitempty"`
}

type Versions struct {
Version string `json:"version,omitempty"`
Channels []string `json:"channels,omitempty"`
ManagerPath string `json:"managerPath,omitempty"`
ManagerImage string `json:"managerImage,omitempty"`
Resources []Resources `json:"resources,omitempty"`
}

type Resources struct {
Spec Spec `json:"spec,omitempty"`
}

type Spec struct {
Group string `json:"group,omitempty"`
Names Names `json:"names,omitempty"`
Scope string `json:"scope,omitempty"`
ApiVersions []ApiVersions `json:"versions,omitempty"`
}

type Names struct {
Kind string `json:"kind,omitempty"`
ListKind string `json:"listKind,omitempty"`
Plural string `json:"plural,omitempty"`
Singular string `json:"singular,omitempty"`
}

type ApiVersions struct {
Name string `json:"name,omitempty"`
}

0 comments on commit dc07ebe

Please sign in to comment.