forked from hooksie1/cmsnr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
133 lines (106 loc) · 2.89 KB
/
watch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/gamer22026/cmsnr/api/v1alpha1"
"github.com/gamer22026/cmsnr/pkg/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
// watchCmd represents the watch command
var watchCmd = &cobra.Command{
Use: "watch",
Short: "Watch will watch the cluster for changes in OPA CRDs.",
Run: watchPolicies,
}
func init() {
opaCmd.AddCommand(watchCmd)
watchCmd.Flags().StringP("deployment", "d", "", "The deployment name of the OPA policy.")
viper.BindPFlag("deployment", watchCmd.Flags().Lookup("deployment"))
watchCmd.MarkFlagRequired("deployment")
}
func watchPolicies(cmd *cobra.Command, args []string) {
deploymentName := viper.GetString("deployment")
v1alpha1.BuildScheme(scheme.Scheme)
config, err := rest.InClusterConfig()
if err != nil {
log.Println(err)
os.Exit(1)
}
client, err := client.NewClient(config, namespace)
if err != nil {
log.Println(err)
os.Exit(1)
}
client.WatchResources()
log.Infof("watching for policy changes with deployment name: %s", deploymentName)
for v := range client.Queue {
if v.OpaPolicy.Spec.DeploymentName != deploymentName {
continue
}
if v.OpaPolicy.Spec.DeploymentName == "" {
continue
}
switch v.Method {
case "add":
log.Infof("found new opa policy %s for deployment %s", v.OpaPolicy.Spec.PolicyName, v.OpaPolicy.Spec.DeploymentName)
if err := updatePolicy(&v.OpaPolicy); err != nil {
log.Error(err)
os.Exit(1)
}
case "update":
log.Infof("found update for opa policy %s for deployment %s", v.OpaPolicy.Spec.PolicyName, v.OpaPolicy.Spec.DeploymentName)
if err := updatePolicy(&v.OpaPolicy); err != nil {
log.Error(err)
os.Exit(1)
}
case "delete":
log.Infof("deleting opa policy %s for deployment %s", v.OpaPolicy.Spec.PolicyName, v.OpaPolicy.Spec.DeploymentName)
if err := deletePolicy(&v.OpaPolicy); err != nil {
log.Error(err)
os.Exit(1)
}
}
time.Sleep(2 * time.Second)
}
close(client.Queue)
}
func updatePolicy(p *v1alpha1.OpaPolicy) error {
policy := p.Spec.Policy
url := fmt.Sprintf("http://localhost:8181/v1/policies/%s", p.Spec.PolicyName)
client := http.Client{}
req, err := http.NewRequest("PUT", url, strings.NewReader(policy))
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("error posting policy")
}
return nil
}
func deletePolicy(p *v1alpha1.OpaPolicy) error {
url := fmt.Sprintf("http://localhost:8181/v1/policies/%s", p.Spec.PolicyName)
client := http.Client{}
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("error deleting policy")
}
return nil
}