Skip to content

Commit 42d823e

Browse files
authored
INS-1526: insights cli kyverno policies cleanup (#287)
* Fix download and validate directories * Fix * Debug * Fixes * Fix * Bump libs * Bump libs * Revert "Bump libs" This reverts commit 1a563ca. * Revert "Bump libs" This reverts commit e8d2922.
1 parent 00797ea commit 42d823e

3 files changed

Lines changed: 40 additions & 26 deletions

File tree

pkg/cli/push_kyverno_policies.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cli
1616

1717
import (
1818
"os"
19-
"path/filepath"
2019

2120
"github.com/fairwindsops/insights-cli/pkg/kyverno"
2221
"github.com/sirupsen/logrus"
@@ -31,7 +30,7 @@ var pushForce bool
3130
const defaultPushKyvernoPoliciesSubDir = "kyverno-policies"
3231

3332
func init() {
34-
pushKyvernoPoliciesCmd.PersistentFlags().StringVarP(&pushKyvernoPoliciesSubDir, "push-kyverno-policies-subdirectory", "", defaultPushKyvernoPoliciesSubDir, "Sub-directory within push-directory, to contain Kyverno policies.")
33+
pushKyvernoPoliciesCmd.PersistentFlags().StringVarP(&pushKyvernoPoliciesSubDir, "push-kyverno-policies-subdirectory", "s", defaultPushKyvernoPoliciesSubDir, "Sub-directory within push-directory, to contain Kyverno policies.")
3534
pushKyvernoPoliciesCmd.PersistentFlags().StringSliceVarP(&pushSpecificPolicies, "policies", "p", []string{}, "Specific policy names to push (e.g., require-labels,disallow-privileged). If not specified, all policies will be pushed.")
3635
pushKyvernoPoliciesCmd.PersistentFlags().BoolVar(&pushSkipValidation, "skip-validation", false, "Skip validation before pushing (not recommended).")
3736
pushKyvernoPoliciesCmd.PersistentFlags().BoolVar(&pushForce, "force", false, "Force push even if validation fails (use with extreme caution).")
@@ -43,29 +42,39 @@ var pushKyvernoPoliciesCmd = &cobra.Command{
4342
Short: "Push Kyverno policies from local files to Insights.",
4443
Long: "Push Kyverno policies from local files to Insights. This command automatically validates all policies before pushing. If ANY validation fails, the push operation is aborted unless --force is used.",
4544
Example: `
46-
# Push all local policies to Insights (with validation)
47-
insights-cli push kyverno-policies -d .
45+
# Push all policies from the default subdirectory
46+
insights-cli push kyverno-policies
4847
49-
# Push specific policies (with validation)
50-
insights-cli push kyverno-policies -d . -p require-labels,disallow-privileged
48+
# Push specific policies from a custom subdirectory
49+
insights-cli push kyverno-policies -s custom-policies -p policy1,policy2
50+
51+
# Push all policies from a custom subdirectory
52+
insights-cli push kyverno-policies -s custom-policies
5153
5254
# Push with dry run to see what would be changed
53-
insights-cli push kyverno-policies -d . --dry-run
55+
insights-cli push kyverno-policies --dry-run
5456
5557
# Skip validation (not recommended)
56-
insights-cli push kyverno-policies -d . --skip-validation
58+
insights-cli push kyverno-policies --skip-validation
5759
5860
# Force push even if validation fails (use with extreme caution)
59-
insights-cli push kyverno-policies -d . --force`,
61+
insights-cli push kyverno-policies --force`,
6062
PreRun: validateAndLoadInsightsAPIConfigWrapper,
6163
Run: func(cmd *cobra.Command, args []string) {
6264
org := configurationObject.Options.Organization
65+
66+
// Check if the push directory exists
67+
_, err := os.Stat(pushDir)
68+
if err != nil {
69+
logrus.Fatalf("Push directory %s does not exist. You need to create it.", pushDir)
70+
}
71+
6372
policyDir := pushDir + "/" + pushKyvernoPoliciesSubDir
6473

65-
// Check if the policy directory exists
66-
_, err := os.Stat(policyDir)
74+
// Check if the policy directory to push exists
75+
_, err = os.Stat(policyDir)
6776
if err != nil {
68-
logrus.Fatalf("Kyverno policy directory %s does not exist. Run 'insights-cli download kyverno-policies -d %s' first to create it.", policyDir, filepath.Dir(policyDir))
77+
logrus.Fatalf("Kyverno push-kyverno-policies-subdirectory %s does not exist. You need to create it.", pushKyvernoPoliciesSubDir)
6978
}
7079

7180
// Get all policy files (excluding test cases)

pkg/kyverno/kyverno_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,20 @@ func TestDisplayClusterValidationResults(t *testing.T) {
181181
// In a real test environment, we'd capture stdout and verify the output
182182
DisplayClusterValidationResults(result)
183183
}
184+
185+
func TestConvertPolicySpecToYAML(t *testing.T) {
186+
policy := KyvernoPolicy{
187+
Name: "test-policy",
188+
Kind: "ClusterPolicy",
189+
APIVersion: "kyverno.io/v1",
190+
Spec: map[string]interface{}{
191+
"metadata": map[string]interface{}{
192+
"name": "test-policy",
193+
},
194+
},
195+
}
196+
197+
yaml, err := convertPolicySpecToYAML(policy)
198+
assert.NoError(t, err)
199+
assert.Equal(t, "apiVersion: kyverno.io/v1\nkind: ClusterPolicy\nmetadata:\n name: test-policy\nspec:\n metadata:\n name: test-policy", yaml)
200+
}

pkg/kyverno/types.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525

2626
// KyvernoPolicy represents a Kyverno policy
2727
type KyvernoPolicy struct {
28-
ID int `json:"id,omitempty"`
29-
OrganizationID int64 `json:"organization_id,omitempty"`
3028
Name string `json:"name" yaml:"metadata.name"`
3129
Kind string `json:"kind" yaml:"kind"`
3230
APIVersion string `json:"api_version" yaml:"apiVersion"`
@@ -49,20 +47,10 @@ func (k KyvernoPolicy) GetYamlBytes() ([]byte, error) {
4947

5048
// Helper function to convert a KyvernoPolicy spec to YAML string
5149
func convertPolicySpecToYAML(policy KyvernoPolicy) (string, error) {
52-
// Parse the spec JSON
53-
54-
apiVersion := "kyverno.io/v1"
55-
kind := "ClusterPolicy"
56-
if policy.Kind != "" {
57-
kind = policy.Kind
58-
}
59-
if policy.APIVersion != "" {
60-
apiVersion = policy.APIVersion
61-
}
6250
// Create the full policy structure
6351
policyMap := map[string]any{
64-
"apiVersion": apiVersion,
65-
"kind": kind,
52+
"apiVersion": policy.APIVersion,
53+
"kind": policy.Kind,
6654
"metadata": map[string]any{
6755
"name": policy.Name,
6856
},

0 commit comments

Comments
 (0)