Skip to content

Commit 086456a

Browse files
authored
Merge pull request #259 from kubescape/reg-check
add check registry operator command
2 parents 7a37d86 + 0108e3f commit 086456a

6 files changed

+35
-95
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/goradd/maps v0.1.5
2323
github.com/gorilla/mux v1.8.1
2424
github.com/gorilla/websocket v1.5.1
25-
github.com/kubescape/backend v0.0.24
25+
github.com/kubescape/backend v0.0.25
2626
github.com/kubescape/go-logger v0.0.22
2727
github.com/kubescape/k8s-interface v0.0.170
2828
github.com/kubescape/kubescape-network-scanner v0.0.15

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,8 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
679679
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
680680
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
681681
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
682-
github.com/kubescape/backend v0.0.24 h1:XKT57u/bIVW+orEXCmRuGNtzQQOAm9DNfa2xfDb6nY0=
683-
github.com/kubescape/backend v0.0.24/go.mod h1:FpazfN+c3Ucuvv4jZYCnk99moSBRNMVIxl5aWCZAEBo=
682+
github.com/kubescape/backend v0.0.25 h1:PLESA7KGJskebR5hiSqPeJ1cPQ8Ra+4yNYXKyIejSKQ=
683+
github.com/kubescape/backend v0.0.25/go.mod h1:FpazfN+c3Ucuvv4jZYCnk99moSBRNMVIxl5aWCZAEBo=
684684
github.com/kubescape/go-logger v0.0.22 h1:gle7wH6emOiGv9ljdpVi82pWLQ3jGucrUucvil6JXHE=
685685
github.com/kubescape/go-logger v0.0.22/go.mod h1:x3HBpZo3cMT/WIdy18BxvVVd5D0e/PWFVk/HiwBNu3g=
686686
github.com/kubescape/k8s-interface v0.0.170 h1:EtzomWoeeIWDz7QrAEsqUDpLHQwoh2m3tZITfrE/tiE=

watcher/commandswatcher.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (cwh *CommandWatchHandler) RegisterForCommands(receiver chan v1alpha1.Opera
4141
}
4242

4343
func (cwh *CommandWatchHandler) CommandWatch(ctx context.Context) {
44+
logger.L().Info("start watching CommandWatchHandler")
4445
// list commands and add them to the queue, this is for the commands that were created before the watch started
4546
cwh.listCommands(ctx)
4647
// start watching

watcher/commandswatcher_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ import (
77
"github.com/armosec/armoapi-go/armotypes"
88
"github.com/kubescape/backend/pkg/command"
99
"github.com/kubescape/backend/pkg/command/types/v1alpha1"
10+
"github.com/kubescape/go-logger"
1011
"github.com/kubescape/k8s-interface/k8sinterface"
1112
"github.com/stretchr/testify/require"
1213
"github.com/testcontainers/testcontainers-go/modules/k3s"
14+
"io"
1315
corev1 "k8s.io/api/core/v1"
1416
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1517
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1618
"k8s.io/apimachinery/pkg/runtime/schema"
1719
"k8s.io/client-go/dynamic"
1820
"k8s.io/client-go/kubernetes"
1921
"k8s.io/client-go/tools/clientcmd"
22+
"net/http"
2023
"sigs.k8s.io/yaml"
2124
"strings"
2225
"testing"
@@ -26,14 +29,12 @@ import (
2629
//go:embed testdata/create-registry-command.json
2730
var createRegistryCommand []byte
2831

29-
//go:embed testdata/operator-command-crd.yaml
30-
var operatorCommandCRD []byte
31-
3232
//go:embed testdata/registry-template-configmap.yaml
3333
var registryTemplateConfiMap []byte
3434

3535
func TestRegistryCommandWatch(t *testing.T) {
3636
ctx := context.Background()
37+
logger.InitDefaultLogger()
3738
terminateFunc, k8sAPI := initK8sClient(t, ctx)
3839
defer terminateFunc()
3940
setupEnvAndWatchers(t, ctx, k8sAPI)
@@ -86,9 +87,15 @@ func TestRegistryCommandWatch(t *testing.T) {
8687

8788
func setupEnvAndWatchers(t *testing.T, ctx context.Context, k8sAPI *k8sinterface.KubernetesApi) {
8889
// install operator command crd
90+
url := "https://raw.githubusercontent.com/kubescape/helm-charts/main/charts/dependency_chart/operatorcommand-crds/crds/operator-command.crd.yaml"
91+
resp, err := http.Get(url)
92+
require.NoError(t, err)
93+
defer resp.Body.Close()
94+
content, err := io.ReadAll(resp.Body)
95+
require.NoError(t, err)
8996
var crd unstructured.Unstructured
90-
require.NoError(t, yaml.Unmarshal(operatorCommandCRD, &crd))
91-
_, err := k8sAPI.DynamicClient.Resource(schema.GroupVersionResource{
97+
require.NoError(t, yaml.Unmarshal(content, &crd))
98+
_, err = k8sAPI.DynamicClient.Resource(schema.GroupVersionResource{
9299
Group: "apiextensions.k8s.io",
93100
Version: "v1",
94101
Resource: "customresourcedefinitions",

watcher/registryhandler.go

+19
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ func NewRegistryCommandsHandler(ctx context.Context, k8sAPI *k8sinterface.Kubern
6565
}
6666

6767
func (ch *RegistryCommandsHandler) Start() {
68+
logger.L().Info("starting RegistryCommandsHandler")
6869
ch.commandsWatcher.RegisterForCommands(ch.commands)
6970

7071
for {
7172
select {
7273
case cmd := <-ch.commands:
7374
if !isRegistryCommand(cmd.Spec.CommandType) {
75+
logger.L().Info("not registry command" + cmd.Spec.CommandType)
7476
continue
7577
}
7678
status := v1alpha1.OperatorCommandStatus{
@@ -79,18 +81,24 @@ func (ch *RegistryCommandsHandler) Start() {
7981
StartedAt: &metav1.Time{Time: time.Now()},
8082
}
8183
var err error
84+
var payload []byte
8285

8386
switch cmd.Spec.CommandType {
8487
case string(command.OperatorCommandTypeCreateRegistry), string(command.OperatorCommandTypeUpdateRegistry):
8588
err = ch.upsertRegistry(cmd)
8689
case string(command.OperatorCommandTypeDeleteRegistry):
8790
err = ch.deleteRegistry(cmd)
91+
case string(command.OperatorCommandTypeCheckRegistry):
92+
payload, err = ch.checkRegistry(cmd)
8893
}
8994

9095
status.Completed = true
9196
status.CompletedAt = &metav1.Time{Time: time.Now()}
97+
9298
if err != nil {
9399
status.Error = &v1alpha1.OperatorCommandStatusError{Message: err.Error()}
100+
} else if len(payload) > 0 {
101+
status.Payload = payload
94102
}
95103
ch.patchCommandStatus(&cmd, status)
96104

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

133+
func (ch *RegistryCommandsHandler) checkRegistry(_ v1alpha1.OperatorCommand) ([]byte, error) {
134+
mockResponse := []string{"mockRepo1", "mockRepo2", "mockRepo3"}
135+
payload, err := json.Marshal(mockResponse)
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
return payload, nil
141+
}
142+
125143
func (ch *RegistryCommandsHandler) deleteRegistry(cmd v1alpha1.OperatorCommand) error {
126144
registry, err := armotypes.UnmarshalRegistry(cmd.Spec.Body)
127145
if err != nil {
@@ -285,6 +303,7 @@ var registryCommands = []string{
285303
string(command.OperatorCommandTypeCreateRegistry),
286304
string(command.OperatorCommandTypeUpdateRegistry),
287305
string(command.OperatorCommandTypeDeleteRegistry),
306+
string(command.OperatorCommandTypeCheckRegistry),
288307
}
289308

290309
func isRegistryCommand(commandType string) bool {

watcher/testdata/operator-command-crd.yaml

-87
This file was deleted.

0 commit comments

Comments
 (0)