-
Notifications
You must be signed in to change notification settings - Fork 15
Replace host sensor with node agent sensing #681
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c525d3a
OSRelease
Bezbran 03eaf69
add the rest of the sensors
Bezbran 07c0190
GetPluralKind
Bezbran 8065ecb
CRDs only in helm-charts repo
Bezbran 9d7e831
Merge branch 'main' into node-agent-replace-host-sensor
Bezbran df0557d
fix TestLoadConfig
Bezbran 9d1811b
Address dear matthyx comments
Bezbran a8e9b0c
Merge branch 'main' into node-agent-replace-host-sensor
Bezbran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,5 +95,7 @@ | |
| "exporters": { | ||
| "stdoutExporter": true | ||
| } | ||
| } | ||
| }, | ||
| "hostSensorEnabled": true, | ||
| "hostSensorInterval": "1m" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package hostsensormanager | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/kubescape/go-logger" | ||
| "github.com/kubescape/go-logger/helpers" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/client-go/dynamic" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| // CRDClient handles Kubernetes CRD operations | ||
| type CRDClient struct { | ||
| dynamicClient dynamic.Interface | ||
| nodeName string | ||
| } | ||
|
|
||
| // NewCRDClient creates a new CRD client | ||
| func NewCRDClient(nodeName string) (*CRDClient, error) { | ||
| config, err := rest.InClusterConfig() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get in-cluster config: %w", err) | ||
| } | ||
|
|
||
| dynamicClient, err := dynamic.NewForConfig(config) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create dynamic client: %w", err) | ||
| } | ||
|
|
||
| return &CRDClient{ | ||
| dynamicClient: dynamicClient, | ||
| nodeName: nodeName, | ||
| }, nil | ||
| } | ||
|
|
||
| // CreateOrUpdateHostData creates or updates a host data CRD | ||
| func (c *CRDClient) CreateOrUpdateHostData(ctx context.Context, resource string, kind string, spec interface{}) error { | ||
|
Bezbran marked this conversation as resolved.
|
||
| gvr := schema.GroupVersionResource{ | ||
| Group: HostDataGroup, | ||
| Version: HostDataVersion, | ||
| Resource: resource, | ||
| } | ||
|
|
||
| // Create the unstructured object | ||
| unstructuredObj := &unstructured.Unstructured{ | ||
| Object: map[string]interface{}{ | ||
| "apiVersion": fmt.Sprintf("%s/%s", HostDataGroup, HostDataVersion), | ||
| "kind": kind, | ||
| "metadata": map[string]interface{}{ | ||
| "name": c.nodeName, | ||
| }, | ||
| "spec": spec, | ||
| "status": map[string]interface{}{ | ||
| "lastSensed": metav1.Now().UTC().Format(time.RFC3339), | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Try to get existing resource | ||
| _, err := c.dynamicClient.Resource(gvr).Get(ctx, c.nodeName, metav1.GetOptions{}) | ||
| if err != nil { | ||
| // Resource doesn't exist, create it | ||
| logger.L().Debug("creating new host data CRD", | ||
| helpers.String("kind", kind), | ||
| helpers.String("nodeName", c.nodeName)) | ||
| _, err = c.dynamicClient.Resource(gvr).Create(ctx, unstructuredObj, metav1.CreateOptions{}) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create %s CRD: %w", kind, err) | ||
| } | ||
| logger.L().Info("created host data CRD", | ||
| helpers.String("kind", kind), | ||
| helpers.String("nodeName", c.nodeName)) | ||
| return nil | ||
| } | ||
|
|
||
| // Resource exists, update it using patch | ||
| logger.L().Debug("updating existing host data CRD", | ||
| helpers.String("kind", kind), | ||
| helpers.String("nodeName", c.nodeName)) | ||
|
|
||
| // Create patch data | ||
| patchData, err := json.Marshal(map[string]interface{}{ | ||
| "spec": spec, | ||
| "status": Status{ | ||
| LastSensed: metav1.Now(), | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal patch data: %w", err) | ||
| } | ||
|
|
||
| _, err = c.dynamicClient.Resource(gvr).Patch(ctx, c.nodeName, types.MergePatchType, patchData, metav1.PatchOptions{}) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to patch %s CRD: %w", kind, err) | ||
| } | ||
|
|
||
| logger.L().Debug("updated host data CRD", | ||
| helpers.String("kind", kind), | ||
| helpers.String("nodeName", c.nodeName)) | ||
| return nil | ||
| } | ||
|
|
||
| // UpdateStatus updates the status of a host data CRD with an error | ||
| func (c *CRDClient) UpdateStatus(ctx context.Context, resource string, errorMsg string) error { | ||
| gvr := schema.GroupVersionResource{ | ||
| Group: HostDataGroup, | ||
| Version: HostDataVersion, | ||
| Resource: resource, | ||
| } | ||
|
|
||
| patchData, err := json.Marshal(map[string]interface{}{ | ||
| "status": Status{ | ||
| LastSensed: metav1.Now(), | ||
| Error: errorMsg, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal patch data: %w", err) | ||
| } | ||
|
|
||
| _, err = c.dynamicClient.Resource(gvr).Patch(ctx, c.nodeName, types.MergePatchType, patchData, metav1.PatchOptions{}) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to update status: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
matthyx marked this conversation as resolved.
|
||
|
|
||
| // toUnstructured converts a typed object to unstructured | ||
| func toUnstructured(obj interface{}) (*unstructured.Unstructured, error) { | ||
| data, err := json.Marshal(obj) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var unstructuredObj unstructured.Unstructured | ||
| err = json.Unmarshal(data, &unstructuredObj.Object) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &unstructuredObj, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.