diff --git a/pkg/command/commands.go b/pkg/command/commands.go new file mode 100644 index 0000000..e246579 --- /dev/null +++ b/pkg/command/commands.go @@ -0,0 +1,29 @@ +package command + +type OperatorCommandType string + +const ( + OperatorCommandAppNameLabelKey string = "kubescape.io/app-name" // holds the app name label, which should execute the command (optional) + OperatorCommandNodeNameLabelKey string = "kubescape.io/node-name" // holds the node name label, app running on this node name should execute the command (optional) + + // command types will be defined here + OperatorCommandTypeRuntimeResponse OperatorCommandType = "RuntimeResponse" +) + +// ResponseCommand +type ResponseAction string + +const ( + ResponseActionKill ResponseAction = "Kill" + ResponseActionStop ResponseAction = "Stop" + ResponseActionPause ResponseAction = "Pause" + ResponseActionUnpause ResponseAction = "Unpause" +) + +type ResponseCommand struct { + Namespace string `json:"namespace,omitempty"` + PodName string `json:"podName,omitempty"` + ContainerName string `json:"containerName,omitempty"` + Pid *uint32 `json:"pid,omitempty"` + Action ResponseAction `json:"action,omitempty"` +} diff --git a/pkg/command/types/api.go b/pkg/command/types/api.go new file mode 100644 index 0000000..573b896 --- /dev/null +++ b/pkg/command/types/api.go @@ -0,0 +1,7 @@ +package types + +const ( + OperatorCommandGroup string = "kubescape.io" + OperatorCommandKind string = "OperatorCommand" + OperatorCommandPlural string = "operatorcommands" +) diff --git a/pkg/command/types/v1alpha1/README.md b/pkg/command/types/v1alpha1/README.md new file mode 100644 index 0000000..8c77caf --- /dev/null +++ b/pkg/command/types/v1alpha1/README.md @@ -0,0 +1,10 @@ +# OperatorCommand + +The OperatorCommand CRD is designed to enable the execution of various actions within the cluster and reporting their status back to the backend. This CRD serves as a central mechanism for triggering and managing actions, replacing the functionality previously provided by the gateway and kollector. + +How it Works + +1. Creation: The backend creates a Command CRD instance, specifying the desired action and any necessary parameters for the action. +2. Synchronization: The Synchronizer, responsible for two-way communication, receives the Command CRD from the backend and saves it in the cluster. +3. Execution: The designated component in the cluster, identifies the new command via a watcher on the Kubernetes API, processes the Command CRD and performs the requested action within the cluster. +4. Status Reporting: Upon completion, the component updates the command CRD resource with the status of the action, providing information about success or failure, any relevant details, and potentially updating the Command CRD. The synchronizer, watching over the command CRD, will send it back to the backend for further processing. diff --git a/pkg/command/types/v1alpha1/api.go b/pkg/command/types/v1alpha1/api.go new file mode 100644 index 0000000..ee1062a --- /dev/null +++ b/pkg/command/types/v1alpha1/api.go @@ -0,0 +1,16 @@ +package v1alpha1 + +import ( + "github.com/kubescape/backend/pkg/command/types" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +const ( + OperatorCommandVersion string = "v1alpha1" +) + +var SchemaGroupVersionResource = schema.GroupVersionResource{ + Group: types.OperatorCommandGroup, + Version: OperatorCommandVersion, + Resource: types.OperatorCommandPlural, +} diff --git a/pkg/command/types/v1alpha1/types.go b/pkg/command/types/v1alpha1/types.go new file mode 100644 index 0000000..6747f78 --- /dev/null +++ b/pkg/command/types/v1alpha1/types.go @@ -0,0 +1,50 @@ +package v1alpha1 + +import ( + "time" + + "github.com/armosec/armoapi-go/identifiers" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type OperatorCommandList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + + Items []OperatorCommand `json:"items"` +} + +type OperatorCommand struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec OperatorCommandSpec `json:"spec,omitempty"` + Status OperatorCommandStatus `json:"status,omitempty"` +} + +type OperatorCommandSpec struct { + GUID string `json:"guid"` // GUID is a unique identifier for the command + CommandType string `json:"commandType"` // CommandType is the type of the command + CommandVersion string `json:"commandVersion,omitempty"` // CommandVersion is the version of the command + Designators []identifiers.PortalDesignator `json:"designators,omitempty"` // Designators are the designators for the command + Body []byte `json:"body,omitempty"` // Body is the body of the command + TTL time.Duration `json:"ttl,omitempty"` // TTL is the time to live for the command + Args map[string]interface{} `json:"args,omitempty"` // Args are the arguments for the command + CommandIndex *int `json:"commandIndex,omitempty"` // CommandIndex is the index of the command in the sequence + CommandCount *int `json:"commandCount,omitempty"` // CommandCount is the total number of commands in the sequence +} + +type OperatorCommandStatus struct { + Started bool `json:"started"` // Started indicates if the command has started + StartedAt *metav1.Time `json:"startedAt,omitempty"` // StartedAt is the time at which the command was started + Completed bool `json:"completed"` // Completed indicates if the command has completed + CompletedAt *metav1.Time `json:"completedAt,omitempty"` // CompletedAt is the time at which the command was completed + Executer string `json:"executer,omitempty"` // Executer is the entity that executed the command + Error *OperatorCommandStatusError `json:"error,omitempty"` // Error is the error that occurred during the execution of the command (if any) +} + +type OperatorCommandStatusError struct { + Reason string `json:"reason,omitempty"` // reason for the error (optional) + Message string `json:"message,omitempty"` // error message (optional) + ErrorCode int `json:"errorCode,omitempty"` // error code (optional) +} diff --git a/pkg/command/utils/utils.go b/pkg/command/utils/utils.go new file mode 100644 index 0000000..8f6a68f --- /dev/null +++ b/pkg/command/utils/utils.go @@ -0,0 +1,28 @@ +package utils + +import ( + "bytes" + "encoding/json" +) + +func EncodeCommandBody[T any](data T) ([]byte, error) { + buffer := bytes.Buffer{} + encoder := json.NewEncoder(&buffer) + err := encoder.Encode(data) + if err != nil { + return nil, err + } + return buffer.Bytes(), nil +} + +func DecodeCommandBody[T any](b []byte) (T, error) { + buffer := bytes.Buffer{} + buffer.Write(b) + decoder := json.NewDecoder(&buffer) + var data T + err := decoder.Decode(&data) + if err != nil { + return data, err + } + return data, nil +} diff --git a/pkg/versioncheck/versioncheck_test.go b/pkg/versioncheck/versioncheck_test.go index 14d531e..0949731 100644 --- a/pkg/versioncheck/versioncheck_test.go +++ b/pkg/versioncheck/versioncheck_test.go @@ -87,7 +87,7 @@ func TestVersionCheckHandler_getLatestVersion(t *testing.T) { }, want: &VersionCheckResponse{ Client: "kubescape", - ClientUpdate: "v3.0.9", + ClientUpdate: "v3.0.15", }, wantErr: false, },