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

add check registry operator command #259

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/goradd/maps v0.1.5
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.1
github.com/kubescape/backend v0.0.24
github.com/kubescape/backend v0.0.25
github.com/kubescape/go-logger v0.0.22
github.com/kubescape/k8s-interface v0.0.170
github.com/kubescape/kubescape-network-scanner v0.0.15
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubescape/backend v0.0.24 h1:XKT57u/bIVW+orEXCmRuGNtzQQOAm9DNfa2xfDb6nY0=
github.com/kubescape/backend v0.0.24/go.mod h1:FpazfN+c3Ucuvv4jZYCnk99moSBRNMVIxl5aWCZAEBo=
github.com/kubescape/backend v0.0.25 h1:PLESA7KGJskebR5hiSqPeJ1cPQ8Ra+4yNYXKyIejSKQ=
github.com/kubescape/backend v0.0.25/go.mod h1:FpazfN+c3Ucuvv4jZYCnk99moSBRNMVIxl5aWCZAEBo=
github.com/kubescape/go-logger v0.0.22 h1:gle7wH6emOiGv9ljdpVi82pWLQ3jGucrUucvil6JXHE=
github.com/kubescape/go-logger v0.0.22/go.mod h1:x3HBpZo3cMT/WIdy18BxvVVd5D0e/PWFVk/HiwBNu3g=
github.com/kubescape/k8s-interface v0.0.170 h1:EtzomWoeeIWDz7QrAEsqUDpLHQwoh2m3tZITfrE/tiE=
Expand Down
1 change: 1 addition & 0 deletions watcher/commandswatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (cwh *CommandWatchHandler) RegisterForCommands(receiver chan v1alpha1.Opera
}

func (cwh *CommandWatchHandler) CommandWatch(ctx context.Context) {
logger.L().Info("start watching CommandWatchHandler")
// list commands and add them to the queue, this is for the commands that were created before the watch started
cwh.listCommands(ctx)
// start watching
Expand Down
17 changes: 12 additions & 5 deletions watcher/commandswatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import (
"github.com/armosec/armoapi-go/armotypes"
"github.com/kubescape/backend/pkg/command"
"github.com/kubescape/backend/pkg/command/types/v1alpha1"
"github.com/kubescape/go-logger"
"github.com/kubescape/k8s-interface/k8sinterface"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/modules/k3s"
"io"
corev1 "k8s.io/api/core/v1"
v1 "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/tools/clientcmd"
"net/http"
"sigs.k8s.io/yaml"
"strings"
"testing"
Expand All @@ -26,14 +29,12 @@ import (
//go:embed testdata/create-registry-command.json
var createRegistryCommand []byte

//go:embed testdata/operator-command-crd.yaml
var operatorCommandCRD []byte

//go:embed testdata/registry-template-configmap.yaml
var registryTemplateConfiMap []byte

func TestRegistryCommandWatch(t *testing.T) {
ctx := context.Background()
logger.InitDefaultLogger()
terminateFunc, k8sAPI := initK8sClient(t, ctx)
defer terminateFunc()
setupEnvAndWatchers(t, ctx, k8sAPI)
Expand Down Expand Up @@ -86,9 +87,15 @@ func TestRegistryCommandWatch(t *testing.T) {

func setupEnvAndWatchers(t *testing.T, ctx context.Context, k8sAPI *k8sinterface.KubernetesApi) {
// install operator command crd
url := "https://raw.githubusercontent.com/kubescape/helm-charts/main/charts/dependency_chart/operatorcommand-crds/crds/operator-command.crd.yaml"
resp, err := http.Get(url)
require.NoError(t, err)
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var crd unstructured.Unstructured
require.NoError(t, yaml.Unmarshal(operatorCommandCRD, &crd))
_, err := k8sAPI.DynamicClient.Resource(schema.GroupVersionResource{
require.NoError(t, yaml.Unmarshal(content, &crd))
_, err = k8sAPI.DynamicClient.Resource(schema.GroupVersionResource{
Group: "apiextensions.k8s.io",
Version: "v1",
Resource: "customresourcedefinitions",
Expand Down
19 changes: 19 additions & 0 deletions watcher/registryhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ func NewRegistryCommandsHandler(ctx context.Context, k8sAPI *k8sinterface.Kubern
}

func (ch *RegistryCommandsHandler) Start() {
logger.L().Info("starting RegistryCommandsHandler")
ch.commandsWatcher.RegisterForCommands(ch.commands)

for {
select {
case cmd := <-ch.commands:
if !isRegistryCommand(cmd.Spec.CommandType) {
logger.L().Info("not registry command" + cmd.Spec.CommandType)
continue
}
status := v1alpha1.OperatorCommandStatus{
Expand All @@ -79,18 +81,24 @@ func (ch *RegistryCommandsHandler) Start() {
StartedAt: &metav1.Time{Time: time.Now()},
}
var err error
var payload []byte

switch cmd.Spec.CommandType {
case string(command.OperatorCommandTypeCreateRegistry), string(command.OperatorCommandTypeUpdateRegistry):
err = ch.upsertRegistry(cmd)
case string(command.OperatorCommandTypeDeleteRegistry):
err = ch.deleteRegistry(cmd)
case string(command.OperatorCommandTypeCheckRegistry):
payload, err = ch.checkRegistry(cmd)
}

status.Completed = true
status.CompletedAt = &metav1.Time{Time: time.Now()}

if err != nil {
status.Error = &v1alpha1.OperatorCommandStatusError{Message: err.Error()}
} else if len(payload) > 0 {
status.Payload = payload
}
ch.patchCommandStatus(&cmd, status)

Expand Down Expand Up @@ -122,6 +130,16 @@ func (ch *RegistryCommandsHandler) patchCommandStatus(command *v1alpha1.Operator
logger.L().Info("patchCommandStatus: command status patched successfully")
}

func (ch *RegistryCommandsHandler) checkRegistry(_ v1alpha1.OperatorCommand) ([]byte, error) {
mockResponse := []string{"mockRepo1", "mockRepo2", "mockRepo3"}
payload, err := json.Marshal(mockResponse)
if err != nil {
return nil, err
}

return payload, nil
}

func (ch *RegistryCommandsHandler) deleteRegistry(cmd v1alpha1.OperatorCommand) error {
registry, err := armotypes.UnmarshalRegistry(cmd.Spec.Body)
if err != nil {
Expand Down Expand Up @@ -285,6 +303,7 @@ var registryCommands = []string{
string(command.OperatorCommandTypeCreateRegistry),
string(command.OperatorCommandTypeUpdateRegistry),
string(command.OperatorCommandTypeDeleteRegistry),
string(command.OperatorCommandTypeCheckRegistry),
}

func isRegistryCommand(commandType string) bool {
Expand Down
87 changes: 0 additions & 87 deletions watcher/testdata/operator-command-crd.yaml

This file was deleted.

Loading