Skip to content

Commit 274fd32

Browse files
authored
Fixes (#288)
1 parent 42d823e commit 274fd32

3 files changed

Lines changed: 23 additions & 90 deletions

File tree

cmd/insights/insights

-54.3 MB
Binary file not shown.

pkg/cli/push_kyverno_policies.go

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,19 @@ import (
2424

2525
var pushKyvernoPoliciesSubDir string
2626
var pushSpecificPolicies []string
27-
var pushSkipValidation bool
28-
var pushForce bool
2927

3028
const defaultPushKyvernoPoliciesSubDir = "kyverno-policies"
3129

3230
func init() {
3331
pushKyvernoPoliciesCmd.PersistentFlags().StringVarP(&pushKyvernoPoliciesSubDir, "push-kyverno-policies-subdirectory", "s", defaultPushKyvernoPoliciesSubDir, "Sub-directory within push-directory, to contain Kyverno policies.")
3432
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.")
35-
pushKyvernoPoliciesCmd.PersistentFlags().BoolVar(&pushSkipValidation, "skip-validation", false, "Skip validation before pushing (not recommended).")
36-
pushKyvernoPoliciesCmd.PersistentFlags().BoolVar(&pushForce, "force", false, "Force push even if validation fails (use with extreme caution).")
3733
pushCmd.AddCommand(pushKyvernoPoliciesCmd)
3834
}
3935

4036
var pushKyvernoPoliciesCmd = &cobra.Command{
4137
Use: "kyverno-policies [-p policy1,policy2]",
4238
Short: "Push Kyverno policies from local files to Insights.",
43-
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.",
39+
Long: "Push Kyverno policies from local files to Insights. We recommend validating policies before pushing. For validating you need to provide samples in the form of .success.yaml and .failure.yaml files.",
4440
Example: `
4541
# Push all policies from the default subdirectory
4642
insights-cli push kyverno-policies
@@ -108,62 +104,6 @@ var pushKyvernoPoliciesCmd = &cobra.Command{
108104
return
109105
}
110106

111-
// Validate policies before pushing (unless skipped or forced)
112-
if !pushSkipValidation {
113-
logrus.Info("Validating policies before push...")
114-
115-
// Discover all policies and test cases for validation
116-
policiesWithTestCases, err := kyverno.DiscoverPoliciesAndTestCases(policyDir)
117-
if err != nil {
118-
logrus.Fatalf("Unable to discover policies for validation: %v", err)
119-
}
120-
121-
// Validate each policy that will be pushed
122-
validationFailed := false
123-
for _, policyToPush := range policiesToPush {
124-
logrus.Infof("Validating policy: %s", policyToPush.Name)
125-
126-
// Find test cases for this policy
127-
var testCases []kyverno.TestResource
128-
for _, policyWithTestCases := range policiesWithTestCases {
129-
if policyWithTestCases.Policy.Name == policyToPush.Name {
130-
testCases = policyWithTestCases.TestCases
131-
break
132-
}
133-
}
134-
135-
// Validate the policy
136-
result, err := kyverno.ValidateKyvernoPolicy(
137-
client, org, policyToPush, testCases, true)
138-
if err != nil {
139-
logrus.Errorf("Unable to validate policy %s: %v", policyToPush.Name, err)
140-
validationFailed = true
141-
continue
142-
}
143-
144-
// Display validation results
145-
displayValidationResults(result, testCases)
146-
if !determineActualValidationResult(result, testCases) {
147-
logrus.Errorf("Policy %s failed validation", policyToPush.Name)
148-
validationFailed = true
149-
} else {
150-
logrus.Infof("Policy %s passed validation", policyToPush.Name)
151-
}
152-
}
153-
154-
// If ANY validation failed, check if force push is enabled
155-
if validationFailed {
156-
if pushForce {
157-
logrus.Warnf("Validation failed but --force flag is set. Proceeding with push anyway...")
158-
logrus.Warnf("WARNING: You are pushing policies that failed validation!")
159-
} else {
160-
logrus.Fatalf("Push aborted: One or more policies failed validation. Please fix the issues before pushing to Insights, or use --force to override.")
161-
}
162-
} else {
163-
logrus.Info("All policies validated successfully!")
164-
}
165-
}
166-
167107
if pushDryRun {
168108
logrus.Infof("Dry run: Would synchronize %d Kyverno policies with Insights:", len(policiesToPush))
169109
for _, policy := range policiesToPush {
@@ -172,9 +112,6 @@ var pushKyvernoPoliciesCmd = &cobra.Command{
172112
if pushDelete {
173113
logrus.Info("Dry run: Would delete policies that exist in Insights but not locally")
174114
}
175-
if pushForce {
176-
logrus.Warnf("Dry run: Force push is enabled - validation failures would be ignored")
177-
}
178115
return
179116
}
180117

@@ -184,10 +121,6 @@ var pushKyvernoPoliciesCmd = &cobra.Command{
184121
logrus.Fatalf("Unable to synchronize kyverno-policies with Insights: %v", err)
185122
}
186123

187-
if pushForce {
188-
logrus.Warnf("Force push completed. Policies have been pushed to Insights despite validation failures.")
189-
} else {
190-
logrus.Infoln("Successfully synchronized kyverno-policies with Insights.")
191-
}
124+
logrus.Infoln("Successfully synchronized kyverno-policies with Insights.")
192125
},
193126
}

pkg/kyverno/types.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ import (
2525

2626
// KyvernoPolicy represents a Kyverno policy
2727
type KyvernoPolicy struct {
28-
Name string `json:"name" yaml:"metadata.name"`
29-
Kind string `json:"kind" yaml:"kind"`
30-
APIVersion string `json:"api_version" yaml:"apiVersion"`
31-
Labels map[string]interface{} `json:"labels,omitempty" yaml:"metadata.labels"`
32-
Annotations map[string]interface{} `json:"annotations,omitempty" yaml:"metadata.annotations"`
33-
Spec map[string]interface{} `json:"spec" yaml:"spec"`
34-
Status map[string]interface{} `json:"status,omitempty"`
35-
ManagedByInsights *bool `json:"managed_by_insights,omitempty" yaml:"managedByInsights,omitempty"`
36-
CreatedAt *time.Time `json:"created_at,omitempty"`
37-
UpdatedAt *time.Time `json:"updated_at,omitempty"`
28+
Name string `json:"name" yaml:"metadata.name"`
29+
Kind string `json:"kind" yaml:"kind"`
30+
APIVersion string `json:"apiVersion" yaml:"apiVersion"`
31+
Labels map[string]any `json:"labels,omitempty" yaml:"metadata.labels"`
32+
Annotations map[string]any `json:"annotations,omitempty" yaml:"metadata.annotations"`
33+
Spec map[string]any `json:"spec" yaml:"spec"`
34+
Status map[string]any `json:"status,omitempty"`
35+
ManagedByInsights *bool `json:"managedByInsights,omitempty" yaml:"managedByInsights,omitempty"`
36+
CreatedAt *time.Time `json:"createdAt,omitempty" yaml:"createdAt,omitempty"`
37+
UpdatedAt *time.Time `json:"updatedAt,omitempty" yaml:"updatedAt,omitempty"`
3838
}
3939

4040
func (k KyvernoPolicy) GetYamlBytes() ([]byte, error) {
@@ -178,14 +178,14 @@ type KyvernoPolicyList struct {
178178

179179
// KyvernoPolicyInput represents the input format expected by the API
180180
type KyvernoPolicyInput struct {
181-
Name string `json:"name"`
182-
Kind string `json:"kind"`
183-
APIVersion string `json:"apiVersion"`
184-
Labels map[string]string `json:"labels,omitempty"`
185-
Annotations map[string]string `json:"annotations,omitempty"`
186-
Spec map[string]interface{} `json:"spec"`
187-
Status *map[string]interface{} `json:"status,omitempty"`
188-
ManagedByInsights *bool `json:"managedByInsights,omitempty"`
181+
Name string `json:"name"`
182+
Kind string `json:"kind"`
183+
APIVersion string `json:"apiVersion"`
184+
Labels map[string]string `json:"labels,omitempty"`
185+
Annotations map[string]string `json:"annotations,omitempty"`
186+
Spec map[string]any `json:"spec"`
187+
Status *map[string]any `json:"status,omitempty"`
188+
ManagedByInsights *bool `json:"managedByInsights,omitempty"`
189189
}
190190

191191
// ValidationRequest represents the request format for policy validation
@@ -196,15 +196,15 @@ type ValidationRequest struct {
196196

197197
// ToKyvernoPolicyInput converts a KyvernoPolicy to KyvernoPolicyInput format
198198
func (k KyvernoPolicy) ToKyvernoPolicyInput() KyvernoPolicyInput {
199-
// Convert labels from map[string]interface{} to map[string]string
199+
// Convert labels from map[string]any to map[string]string
200200
labels := make(map[string]string)
201201
for key, value := range k.Labels {
202202
if str, ok := value.(string); ok {
203203
labels[key] = str
204204
}
205205
}
206206

207-
// Convert annotations from map[string]interface{} to map[string]string
207+
// Convert annotations from map[string]any to map[string]string
208208
annotations := make(map[string]string)
209209
for key, value := range k.Annotations {
210210
if str, ok := value.(string); ok {
@@ -213,7 +213,7 @@ func (k KyvernoPolicy) ToKyvernoPolicyInput() KyvernoPolicyInput {
213213
}
214214

215215
// Convert status to pointer if it exists
216-
var status *map[string]interface{}
216+
var status *map[string]any
217217
if k.Status != nil {
218218
status = &k.Status
219219
}

0 commit comments

Comments
 (0)