2020-01-07 10:33:28 -08:00
|
|
|
package generate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/glog"
|
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
|
|
|
kyvernoclient "github.com/nirmata/kyverno/pkg/client/clientset/versioned"
|
|
|
|
)
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//StatusControlInterface provides interface to update status subresource
|
2020-01-07 10:33:28 -08:00
|
|
|
type StatusControlInterface interface {
|
2020-01-07 15:13:57 -08:00
|
|
|
Failed(gr kyverno.GenerateRequest, message string, genResources []kyverno.ResourceSpec) error
|
|
|
|
Success(gr kyverno.GenerateRequest, genResources []kyverno.ResourceSpec) error
|
2020-01-07 10:33:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatusControl is default implementaation of GRStatusControlInterface
|
|
|
|
type StatusControl struct {
|
|
|
|
client kyvernoclient.Interface
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//Failed sets gr status.state to failed with message
|
2020-01-07 15:13:57 -08:00
|
|
|
func (sc StatusControl) Failed(gr kyverno.GenerateRequest, message string, genResources []kyverno.ResourceSpec) error {
|
2020-01-07 10:33:28 -08:00
|
|
|
gr.Status.State = kyverno.Failed
|
|
|
|
gr.Status.Message = message
|
2020-01-07 15:13:57 -08:00
|
|
|
// Update Generated Resources
|
|
|
|
gr.Status.GeneratedResources = genResources
|
2020-01-07 10:33:28 -08:00
|
|
|
_, err := sc.client.KyvernoV1().GenerateRequests("kyverno").UpdateStatus(&gr)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("FAILED: updated gr %s status to %s", gr.Name, string(kyverno.Failed))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("updated gr %s status to %s", gr.Name, string(kyverno.Failed))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
// Success sets the gr status.state to completed and clears message
|
2020-01-07 15:13:57 -08:00
|
|
|
func (sc StatusControl) Success(gr kyverno.GenerateRequest, genResources []kyverno.ResourceSpec) error {
|
2020-01-07 10:33:28 -08:00
|
|
|
gr.Status.State = kyverno.Completed
|
|
|
|
gr.Status.Message = ""
|
2020-01-07 15:13:57 -08:00
|
|
|
// Update Generated Resources
|
|
|
|
gr.Status.GeneratedResources = genResources
|
2020-01-07 10:33:28 -08:00
|
|
|
|
|
|
|
_, err := sc.client.KyvernoV1().GenerateRequests("kyverno").UpdateStatus(&gr)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("FAILED: updated gr %s status to %s", gr.Name, string(kyverno.Completed))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("updated gr %s status to %s", gr.Name, string(kyverno.Completed))
|
|
|
|
return nil
|
|
|
|
}
|