|
| 1 | +package ocm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/openshift/insights-operator/pkg/config" |
| 10 | + v1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" |
| 14 | + "k8s.io/klog/v2" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + targetNamespaceName = "openshift-config-managed" |
| 19 | + secretName = "simple-content-access-cert" |
| 20 | +) |
| 21 | + |
| 22 | +// Controller holds all the required resources to be able to communicate with OCM API |
| 23 | +type Controller struct { |
| 24 | + coreClient corev1client.CoreV1Interface |
| 25 | + context context.Context |
| 26 | + configurator Configurator |
| 27 | +} |
| 28 | + |
| 29 | +// Configurator represents the interface to retrieve the configuration for the gatherer |
| 30 | +type Configurator interface { |
| 31 | + Config() *config.Controller |
| 32 | + ConfigChanged() (<-chan struct{}, func()) |
| 33 | +} |
| 34 | + |
| 35 | +// New creates new instance |
| 36 | +func New(ctx context.Context, coreClient corev1client.CoreV1Interface, configurator Configurator) *Controller { |
| 37 | + return &Controller{ |
| 38 | + coreClient: coreClient, |
| 39 | + context: ctx, |
| 40 | + configurator: configurator, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Run periodically queries the OCM API and update corresponding secret accordingly |
| 45 | +func (c *Controller) Run() { |
| 46 | + cfg := c.configurator.Config() |
| 47 | + endpoint := cfg.OcmEndpoint |
| 48 | + interval := cfg.OcmInterval |
| 49 | + configCh, cancel := c.configurator.ConfigChanged() |
| 50 | + defer cancel() |
| 51 | + |
| 52 | + c.requestDataAndCheckSecret(endpoint) |
| 53 | + for { |
| 54 | + select { |
| 55 | + case <-time.After(interval): |
| 56 | + c.requestDataAndCheckSecret(endpoint) |
| 57 | + case <-configCh: |
| 58 | + cfg := c.configurator.Config() |
| 59 | + interval = cfg.OcmInterval |
| 60 | + endpoint = cfg.OcmEndpoint |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (c *Controller) requestDataAndCheckSecret(endpoint string) { |
| 66 | + data, err := requestData(endpoint) |
| 67 | + if err != nil { |
| 68 | + klog.Errorf("errror requesting data from %s: %v", endpoint, err) |
| 69 | + } |
| 70 | + // check & update the secret here |
| 71 | + ok, err := c.checkSecret(data) |
| 72 | + if !ok { |
| 73 | + // TODO - change IO status in case of failure ? |
| 74 | + klog.Errorf("error when checking the %s secret: %v", secretName, err) |
| 75 | + return |
| 76 | + } |
| 77 | + klog.Infof("%s secret successfuly updated", secretName) |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +// checkSecret checks "simple-content-access" secret in the "openshift-config-managed" namespace. |
| 82 | +// If the secret doesn't exist then it will create a new one. |
| 83 | +// If the secret already exist then it will update the data. |
| 84 | +func (c *Controller) checkSecret(data []byte) (bool, error) { |
| 85 | + scaSec, err := c.coreClient.Secrets(targetNamespaceName).Get(c.context, secretName, metav1.GetOptions{}) |
| 86 | + |
| 87 | + //if the secret doesn't exist then create one |
| 88 | + if errors.IsNotFound(err) { |
| 89 | + _, err = c.createSecret(data) |
| 90 | + if err != nil { |
| 91 | + return false, err |
| 92 | + } |
| 93 | + return true, nil |
| 94 | + } |
| 95 | + if err != nil { |
| 96 | + return false, err |
| 97 | + } |
| 98 | + |
| 99 | + _, err = c.updateSecret(scaSec, data) |
| 100 | + if err != nil { |
| 101 | + return false, err |
| 102 | + } |
| 103 | + return true, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (o *Controller) createSecret(data []byte) (*v1.Secret, error) { |
| 107 | + newSCA := &v1.Secret{ |
| 108 | + ObjectMeta: metav1.ObjectMeta{ |
| 109 | + Name: secretName, |
| 110 | + Namespace: targetNamespaceName, |
| 111 | + }, |
| 112 | + // TODO set the data correctly |
| 113 | + Data: map[string][]byte{ |
| 114 | + v1.TLSCertKey: data, |
| 115 | + v1.TLSPrivateKeyKey: data, |
| 116 | + }, |
| 117 | + Type: v1.SecretTypeTLS, |
| 118 | + } |
| 119 | + cm, err := o.coreClient.Secrets(targetNamespaceName).Create(o.context, newSCA, metav1.CreateOptions{}) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + return cm, nil |
| 124 | +} |
| 125 | + |
| 126 | +// updateSecret updates provided secret with given data |
| 127 | +func (o *Controller) updateSecret(s *v1.Secret, data []byte) (*v1.Secret, error) { |
| 128 | + |
| 129 | + // TODO set the data correctly |
| 130 | + s.Data = map[string][]byte{ |
| 131 | + v1.TLSCertKey: data, |
| 132 | + v1.TLSPrivateKeyKey: data, |
| 133 | + } |
| 134 | + s, err := o.coreClient.Secrets(s.Namespace).Update(o.context, s, metav1.UpdateOptions{}) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + return s, nil |
| 139 | +} |
| 140 | + |
| 141 | +// TODO |
| 142 | +// - no need to create new HTTP client every time |
| 143 | +// - add authorization |
| 144 | +// - add response status check |
| 145 | +// - move this to insightsclient? |
| 146 | +func requestData(endpoint string) ([]byte, error) { |
| 147 | + req, err := http.NewRequest(http.MethodGet, endpoint, nil) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + client := &http.Client{} |
| 152 | + res, err := client.Do(req) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + d, err := ioutil.ReadAll(res.Body) |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + return d, nil |
| 161 | +} |
0 commit comments