Skip to content

Commit

Permalink
Merge pull request kubescape#1203 from kubescape/fix/remove-outdated-…
Browse files Browse the repository at this point in the history
…endpoints

fix: remove outdated enpoints
  • Loading branch information
David Wertenteil committed Apr 27, 2023
2 parents dd961b9 + b48c04d commit a73081c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 57 deletions.
2 changes: 1 addition & 1 deletion core/pkg/hostsensorutils/hostsensor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
- operator: Exists
containers:
- name: host-sensor
image: quay.io/kubescape/host-scanner:v1.0.54
image: quay.io/kubescape/host-scanner:v1.0.57
securityContext:
allowPrivilegeEscalation: true
privileged: true
Expand Down
35 changes: 2 additions & 33 deletions core/pkg/hostsensorutils/hostsensordeploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestHostSensorHandler(t *testing.T) {
envelope, status, err := h.CollectResources(ctx)
require.NoError(t, err)

require.Len(t, envelope, 11*2) // has cloud provider, no control plane requested
require.Len(t, envelope, 10*2) // has cloud provider, no control plane requested
require.Len(t, status, 0)

foundControl, foundProvider := false, false
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestHostSensorHandler(t *testing.T) {
envelope, status, err := h.CollectResources(ctx)
require.NoError(t, err)

require.Len(t, envelope, 12*2) // has empty cloud provider, has control plane info
require.Len(t, envelope, 11*2) // has empty cloud provider, has control plane info
require.Len(t, status, 0)

foundControl, foundProvider := false, false
Expand Down Expand Up @@ -141,37 +141,6 @@ func TestHostSensorHandler(t *testing.T) {
})
})

t.Run("should build host sensor with error in response from /kubeletConfigurations", func(t *testing.T) {
k8s := NewKubernetesApiMock(WithNode(mockNode1()),
WithPod(mockPod1()),
WithPod(mockPod2()),
WithResponses(mockResponsesNoCloudProvider()),
WithErrorResponse(RestURL{"http", "pod1", "7888", "/kubeletConfigurations"}), // this endpoint will return an error from this pod
)

h, err := NewHostSensorHandler(k8s, "")
require.NoError(t, err)
require.NotNil(t, h)

t.Run("should initialize host sensor", func(t *testing.T) {
require.NoError(t, h.Init(ctx))

w, err := k8s.KubernetesClient.CoreV1().Pods(h.daemonSet.Namespace).Watch(ctx, metav1.ListOptions{})
require.NoError(t, err)
w.Stop()

require.Len(t, h.hostSensorPodNames, 2)
})

t.Run("should collect resources from pods, with some errors", func(t *testing.T) {
envelope, status, err := h.CollectResources(ctx)
require.NoError(t, err)

require.Len(t, envelope, 12*2-1) // one resource is missing
require.Len(t, status, 0) // error is not reported in status: this is due to the worker pool not bubbling up errors
})
})

t.Run("should FAIL to build host sensor because there are no nodes", func(t *testing.T) {
h, err := NewHostSensorHandler(NewKubernetesApiMock(), "")
require.Error(t, err)
Expand Down
23 changes: 0 additions & 23 deletions core/pkg/hostsensorutils/hostsensorgetfrompod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ import (
"sync"

logger "github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/kubescape/k8s-interface/k8sinterface"
"github.com/kubescape/opa-utils/objectsenvelopes/hostsensor"
"github.com/kubescape/opa-utils/reporthandling/apis"

"sigs.k8s.io/yaml"
)

// getPodList clones the internal list of pods being watched as a map of pod names.
Expand Down Expand Up @@ -204,22 +201,6 @@ func (hsh *HostSensorHandler) getOsReleaseFile(ctx context.Context) ([]hostsenso
return hsh.sendAllPodsHTTPGETRequest(ctx, "/osRelease", "OsReleaseFile")
}

// getKubeletConfigurations returns the list of kubelet configurations.
func (hsh *HostSensorHandler) getKubeletConfigurations(ctx context.Context) ([]hostsensor.HostSensorDataEnvelope, error) {
// loop over pods and port-forward it to each of them
res, err := hsh.sendAllPodsHTTPGETRequest(ctx, "/kubeletConfigurations", "KubeletConfiguration") // empty kind, will be overridden
for resIdx := range res {
jsonBytes, ery := yaml.YAMLToJSON(res[resIdx].Data)
if ery != nil {
logger.L().Ctx(ctx).Warning("failed to convert kubelet configurations from yaml to json", helpers.Error(ery))
continue
}
res[resIdx].SetData(jsonBytes)
}

return res, err
}

// hasCloudProviderInfo iterates over the []hostsensor.HostSensorDataEnvelope list to find info about the cloud provider.
//
// If information are found, then return true. Return false otherwise.
Expand Down Expand Up @@ -259,10 +240,6 @@ func (hsh *HostSensorHandler) CollectResources(ctx context.Context) ([]hostsenso
Query func(context.Context) ([]hostsensor.HostSensorDataEnvelope, error)
}{
// queries to the deployed host-scanner
{
Resource: KubeletConfiguration,
Query: hsh.getKubeletConfigurations,
},
{
Resource: KubeletCommandLine,
Query: hsh.getKubeletCommandLine,
Expand Down

0 comments on commit a73081c

Please sign in to comment.