Skip to content

Commit 79982aa

Browse files
committed
add check registry operator command
Signed-off-by: refaelm <[email protected]>
1 parent 7a37d86 commit 79982aa

6 files changed

+30
-4
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

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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"
@@ -34,6 +35,7 @@ var registryTemplateConfiMap []byte
3435

3536
func TestRegistryCommandWatch(t *testing.T) {
3637
ctx := context.Background()
38+
logger.InitDefaultLogger()
3739
terminateFunc, k8sAPI := initK8sClient(t, ctx)
3840
defer terminateFunc()
3941
setupEnvAndWatchers(t, ctx, k8sAPI)

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,9 @@ spec:
8383
errorCode:
8484
type: integer
8585
nullable: true
86+
payload:
87+
type: string
88+
format: byte
89+
nullable: true
8690
subresources:
87-
status: {}
91+
status: {}

0 commit comments

Comments
 (0)