-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,4 @@ | ||
[ | ||
"100-psp", | ||
"next-psp-name" | ||
] |
This file contains 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 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,43 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/openshift/insights-operator/pkg/record" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
policyclient "k8s.io/client-go/kubernetes/typed/policy/v1beta1" | ||
) | ||
|
||
// GatherPodSecurityPolicies gathers the names of installed PodSecurityPolicies | ||
// | ||
// The Kubernetes API https://github.com/kubernetes/client-go/blob/v12.0.0/kubernetes/typed/policy/v1beta1/podsecuritypolicy.go#L76 | ||
// | ||
// * Location in archive: config/psp_names.json | ||
// * See: docs/insights-archive-sample/config/psp_names.json | ||
// * Id in config: psps | ||
// * Since versions: | ||
// * 4.10+ | ||
func (g *Gatherer) GatherPodSecurityPolicies(ctx context.Context) ([]record.Record, []error) { | ||
gatherPolicyClient, err := policyclient.NewForConfig(g.gatherKubeConfig) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
return gatherPodSecurityPolicies(ctx, gatherPolicyClient) | ||
} | ||
|
||
func gatherPodSecurityPolicies(ctx context.Context, policyClient policyclient.PolicyV1beta1Interface) ([]record.Record, []error) { | ||
psps, err := policyClient.PodSecurityPolicies().List(ctx, metav1.ListOptions{}) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
pspNames := make([]string, 0, len(psps.Items)) | ||
for i := range psps.Items { | ||
psp := psps.Items[i] | ||
pspNames = append(pspNames, psp.Name) | ||
} | ||
return []record.Record{{ | ||
Name: "config/psp_names", | ||
Item: record.JSONMarshaller{Object: pspNames}, | ||
}}, nil | ||
} |
This file contains 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,47 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/openshift/insights-operator/pkg/record" | ||
"github.com/stretchr/testify/assert" | ||
policyv1beta1 "k8s.io/api/policy/v1beta1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
kubefake "k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
var ( | ||
psp1 *policyv1beta1.PodSecurityPolicy = &policyv1beta1.PodSecurityPolicy{ | ||
ObjectMeta: v1.ObjectMeta{Name: "psp-1"}, | ||
} | ||
psp2 *policyv1beta1.PodSecurityPolicy = &policyv1beta1.PodSecurityPolicy{ | ||
ObjectMeta: v1.ObjectMeta{Name: "psp-2"}, | ||
} | ||
) | ||
|
||
func Test_PodSecurityPolicies_Gather(t *testing.T) { | ||
coreClient := kubefake.NewSimpleClientset() | ||
ctx := context.Background() | ||
records, errs := gatherPodSecurityPolicies(ctx, coreClient.PolicyV1beta1()) | ||
assert.Empty(t, errs, "Unexpected errors: %#v", errs) | ||
assert.Len(t, records, 1) | ||
s, ok := records[0].Item.(record.JSONMarshaller).Object.([]string) | ||
assert.True(t, ok, "Unexpected data format. Expecting an array of strings") | ||
assert.Equal(t, s, []string{}, "Expecting an empty array") | ||
|
||
// create some psps | ||
_, err := coreClient.PolicyV1beta1().PodSecurityPolicies().Create(ctx, psp1, v1.CreateOptions{}) | ||
assert.NoError(t, err, "Unexpected error when creating test PodSecurityPolicy") | ||
_, err = coreClient.PolicyV1beta1().PodSecurityPolicies().Create(ctx, psp2, v1.CreateOptions{}) | ||
assert.NoError(t, err, "Unexpected error when creating test PodSecurityPolicy") | ||
|
||
// check that the created PSPs are actually gathered | ||
records, errs = gatherPodSecurityPolicies(ctx, coreClient.PolicyV1beta1()) | ||
assert.Empty(t, errs, "Unexpected errors: %#v", errs) | ||
assert.Len(t, records, 1) | ||
|
||
s, ok = records[0].Item.(record.JSONMarshaller).Object.([]string) | ||
assert.True(t, ok, "Unexpected data format. Expecting an array of strings") | ||
assert.Equal(t, s, []string{"psp-1", "psp-2"}, "Expecting an empty array") | ||
} |