ESonata commented on a change in pull request #31:
URL: https://github.com/apache/skywalking-swck/pull/31#discussion_r724841471



##########
File path: 
pkg/operator/manifests/storage/elasticsearch7/templates/statefulset.yaml
##########
@@ -0,0 +1,97 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you under
+# the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+apiVersion: apps/v1
+kind: StatefulSet
+metadata:
+  name: {{ .Name }}-elasticsearch7
+  namespace: {{ .Namespace }}
+  labels:
+    app: es
+    operator.skywalking.apache.org/es-name: {{ .Name }}
+    operator.skywalking.apache.org/application: elasticsearch
+    operator.skywalking.apache.org/component: statefulset
+spec:
+  serviceName:  {{ .Spec.ServiceName }}
+  replicas: {{ .Spec.Instances }}
+  selector:
+    matchLabels:
+      app: es
+      operator.skywalking.apache.org/es-name: {{ .Name }}
+  template:
+    metadata:
+      labels:
+        app: es
+        operator.skywalking.apache.org/es-name: {{ .Name }}
+        operator.skywalking.apache.org/application: elasticsearch
+        operator.skywalking.apache.org/component: statefulset
+    spec:
+      serviceAccountName: {{ .Name }}-elasticsearch7
+      nodeName: n3
+      containers:
+        - name: elasticsearch
+          image: {{ .Spec.Image }}
+          resources:
+            limits:
+              cpu: 1000m
+            requests:
+              cpu: 100m
+          imagePullPolicy: IfNotPresent
+          ports:
+            - containerPort: 9200
+              name: rest
+              protocol: TCP
+            - containerPort: 9300
+              name: inter-node
+              protocol: TCP
+          volumeMounts:
+            - name: config
+              mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
+              subPath: elasticsearch.yml
+            {{ if .Spec.Security.User.SecretName }}
+            - name: cert
+              mountPath: "/usr/share/elasticsearch/config/storage.p12"
+              subPath: storage.p12
+            {{end}}
+          env:
+            - name: cluster.name
+              value: "{{ .Name }}-skywalking-es"
+            - name: node.name
+              valueFrom:
+                fieldRef:
+                  fieldPath: metadata.name
+            - name: cluster.initial_master_nodes
+              value: "{{ .Name }}-elasticsearch7-0,{{ .Name 
}}-elasticsearch7-1"
+            - name: ES_JAVA_OPTS
+              value: "-Xms512m -Xmx512m"
+            {{ range .Spec.Config }}

Review comment:
       I set thread_pool.index.queue_size, but elasticsearch can’t start 
normally.

##########
File path: controllers/operator/storage_controller.go
##########
@@ -0,0 +1,274 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package controllers
+
+import (
+       "bytes"
+       "context"
+       "crypto/rand"
+       "crypto/rsa"
+       "crypto/x509"
+       "crypto/x509/pkix"
+       "encoding/asn1"
+       "encoding/pem"
+       "fmt"
+
+       "github.com/go-logr/logr"
+       apps "k8s.io/api/apps/v1"
+       certv1beta1 "k8s.io/api/certificates/v1beta1"
+       core "k8s.io/api/core/v1"
+       apiequal "k8s.io/apimachinery/pkg/api/equality"
+       apierrors "k8s.io/apimachinery/pkg/api/errors"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+       "k8s.io/apimachinery/pkg/runtime"
+       kubeclient "k8s.io/client-go/kubernetes"
+       "k8s.io/client-go/rest"
+       "k8s.io/client-go/tools/record"
+       ctrl "sigs.k8s.io/controller-runtime"
+       "sigs.k8s.io/controller-runtime/pkg/client"
+       "software.sslmate.com/src/go-pkcs12"
+
+       operatorv1alpha1 
"github.com/apache/skywalking-swck/apis/operator/v1alpha1"
+       "github.com/apache/skywalking-swck/pkg/kubernetes"
+)
+
+// StorageReconciler reconciles a Storage object
+type StorageReconciler struct {
+       client.Client
+       Log        logr.Logger
+       Scheme     *runtime.Scheme
+       FileRepo   kubernetes.Repo
+       Recorder   record.EventRecorder
+       RestConfig *rest.Config
+}
+
+// 
+kubebuilder:rbac:groups=operator.skywalking.apache.org,resources=storages,verbs=get;list;watch;create;update;patch;delete
+// 
+kubebuilder:rbac:groups=operator.skywalking.apache.org,resources=storages/status,verbs=get;update;patch
+// 
+kubebuilder:rbac:groups=apps,resources=statefulset,verbs=get;list;watch;create;update;patch;delete
+// 
+kubebuilder:rbac:groups="",resources=services;serviceaccounts,verbs=get;list;watch;create;update;patch;delete
+// 
+kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;create;update
+// 
+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterroles;clusterrolebindings,verbs=*
+
+func (r *StorageReconciler) Reconcile(ctx context.Context, req ctrl.Request) 
(ctrl.Result, error) {
+       log := r.Log.WithValues("Storage", req.NamespacedName)
+       log.Info("=====================reconcile 
started================================")
+
+       storage := operatorv1alpha1.Storage{}
+       if err := r.Client.Get(ctx, req.NamespacedName, &storage); err != nil {
+               return ctrl.Result{}, client.IgnoreNotFound(err)
+       }
+       if storage.Spec.ConnectType == "external" {
+               return ctrl.Result{RequeueAfter: schedDuration}, nil
+       }
+       if storage.Spec.Type == "elasticsearch7" {
+               r.createCert(ctx, log, &storage)
+               r.checkSecurity(ctx, log, &storage)
+       }
+       ff, err := r.FileRepo.GetFilesRecursive(storage.Spec.Type + 
"/templates")
+       if err != nil {
+               log.Error(err, "failed to load resource templates")
+               return ctrl.Result{}, err
+       }
+       app := kubernetes.Application{
+               Client:   r.Client,
+               FileRepo: r.FileRepo,
+               CR:       &storage,
+               GVK:      operatorv1alpha1.GroupVersion.WithKind("Storage"),
+               Recorder: r.Recorder,
+       }
+       if err := app.ApplyAll(ctx, ff, log); err != nil {
+               return ctrl.Result{}, err
+       }
+       if err := r.checkState(ctx, log, &storage); err != nil {
+               log.Error(err, "failed to check sub resources state")
+               return ctrl.Result{}, err
+       }
+
+       return ctrl.Result{RequeueAfter: schedDuration}, nil
+}
+
+func (r *StorageReconciler) checkState(ctx context.Context, log logr.Logger, 
storage *operatorv1alpha1.Storage) error {
+       overlay := operatorv1alpha1.StorageStatus{}
+       statefulset := apps.StatefulSet{}
+       errCol := new(kubernetes.ErrorCollector)
+       object := client.ObjectKey{Namespace: storage.Namespace, Name: 
storage.Name + "-" + storage.Spec.Type}
+       if err := r.Client.Get(ctx, object, &statefulset); err != nil && 
!apierrors.IsNotFound(err) {
+               errCol.Collect(fmt.Errorf("failed to get statefulset: %w", err))
+       } else {
+               overlay.Conditions = statefulset.Status.Conditions
+       }
+
+       if apiequal.Semantic.DeepDerivative(overlay, storage.Status) {
+               log.Info("Status keeps the same as before")
+       }
+       storage.Status = overlay
+       storage.Kind = "Storage"
+       if err := kubernetes.ApplyOverlay(storage, 
&operatorv1alpha1.Storage{Status: overlay}); err != nil {
+               errCol.Collect(fmt.Errorf("failed to apply overlay: %w", err))
+               return errCol.Error()
+       }
+       if err := r.Status().Update(ctx, storage); err != nil {
+               errCol.Collect(fmt.Errorf("failed to update status of es: %w", 
err))
+       }
+       log.Info("updated Status sub resource")
+       return errCol.Error()
+}
+
+func (r *StorageReconciler) checkSecurity(ctx context.Context, log 
logr.Logger, s *operatorv1alpha1.Storage) {
+       user, tls := s.Spec.Security.User, s.Spec.Security.TLS
+       if user.SecretName != "" {
+               if user.SecretName == "default" {
+                       s.Spec.Config = append(s.Spec.Config, core.EnvVar{Name: 
"ELASTIC_USER", Value: "elastic"})
+                       s.Spec.Config = append(s.Spec.Config, core.EnvVar{Name: 
"ELASTIC_PASSWORD", Value: "changeme"})
+               } else {
+                       usersecret := core.Secret{}
+                       if err := r.Client.Get(ctx, client.ObjectKey{Namespace: 
s.Namespace, Name: user.SecretName}, &usersecret); err != nil && 
!apierrors.IsNotFound(err) {
+                               log.Info("fail get usersecret ")
+                       }
+                       for k, v := range usersecret.Data {
+                               if k == "username" {
+                                       s.Spec.Config = append(s.Spec.Config, 
core.EnvVar{Name: "ELASTIC_USER", Value: string(v)})
+                               } else if k == "password" {
+                                       s.Spec.Config = append(s.Spec.Config, 
core.EnvVar{Name: "ELASTIC_PASSWORD", Value: string(v)})
+                               }
+                       }
+               }
+       }
+       if tls {
+               s.Spec.ServiceName = "skywalking-storage"
+       } else {
+               s.Spec.ServiceName = s.Name + "-" + s.Spec.Type
+       }
+       s.Spec.Config = append(s.Spec.Config, core.EnvVar{Name: 
"discovery.seed_hosts", Value: s.Spec.ServiceName})
+}
+
+func (r *StorageReconciler) createCert(ctx context.Context, log logr.Logger, s 
*operatorv1alpha1.Storage) {
+       clientset, err := kubeclient.NewForConfig(r.RestConfig)
+       if err != nil {
+               return
+       }
+       secret2, _ := clientset.CoreV1().Secrets(s.Namespace).Get(ctx, 
"skywalking-storage", metav1.GetOptions{})
+       if secret2.Name != "" {
+               return
+       }
+       key, _ := rsa.GenerateKey(rand.Reader, 4096)
+       subj := pkix.Name{
+               CommonName:         "skywalking-storage",
+               Country:            []string{"CN"},
+               Province:           []string{"ZJ"},
+               Locality:           []string{"HZ"},
+               Organization:       []string{"Skywalking"},
+               OrganizationalUnit: []string{"Skywalking"},
+       }
+       asn1Subj, err := asn1.Marshal(subj.ToRDNSequence())
+       if err != nil {
+               return
+       }
+       template := x509.CertificateRequest{
+               RawSubject:         asn1Subj,
+               SignatureAlgorithm: x509.SHA256WithRSA,
+       }
+       csrBytes, _ := x509.CreateCertificateRequest(rand.Reader, &template, 
key)
+       buffer := new(bytes.Buffer)
+       err = pem.Encode(buffer, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: 
csrBytes})
+       if err != nil {
+               return
+       }
+       singername := "kubernetes.io/kube-apiserver-client"
+       request := certv1beta1.CertificateSigningRequest{
+               TypeMeta: metav1.TypeMeta{
+                       Kind:       "CertificateSigningRequest",
+                       APIVersion: "certificates.k8s.io/v1beta1",
+               },
+               ObjectMeta: metav1.ObjectMeta{
+                       Name: "storage-csr",
+               },
+               Spec: certv1beta1.CertificateSigningRequestSpec{
+                       Groups:     []string{"system:authenticated"},
+                       Request:    buffer.Bytes(),
+                       SignerName: &singername,
+                       Usages:     
[]certv1beta1.KeyUsage{certv1beta1.UsageClientAuth},
+               },
+       }
+       err = 
clientset.CertificatesV1beta1().CertificateSigningRequests().Delete(ctx, 
"storage-csr", metav1.DeleteOptions{})
+       if err != nil {
+               log.Info("fail delete csr")
+               return
+       }
+       csr, err := 
clientset.CertificatesV1beta1().CertificateSigningRequests().Create(ctx, 
&request, metav1.CreateOptions{})
+       if err != nil {
+               log.Info("fail create csr")
+               return
+       }
+       condition := certv1beta1.CertificateSigningRequestCondition{
+               Type:    "Approved",
+               Reason:  "ApprovedBySkywalkingStorage",
+               Message: "Approved by skywalking storage controller",
+       }
+       csr.Status.Conditions = append(csr.Status.Conditions, condition)
+       updateapproval, err := 
clientset.CertificatesV1beta1().CertificateSigningRequests().UpdateApproval(ctx,
 csr, metav1.UpdateOptions{})
+       if err != nil {
+               log.Info("fail update approval:", updateapproval)
+               return
+       }
+       for {
+               csr, err = 
clientset.CertificatesV1beta1().CertificateSigningRequests().Get(ctx, 
"storage-csr", metav1.GetOptions{})
+               if err != nil {
+                       log.Info("fail get storage-csr")
+                       return
+               }
+               if csr.Status.Certificate != nil {
+                       break
+               }
+       }
+       block, _ := pem.Decode(csr.Status.Certificate)
+       cert, _ := x509.ParseCertificate(block.Bytes)
+       p12, _ := pkcs12.Encode(rand.Reader, key, cert, nil, "")

Review comment:
       I fixed the error returned on lines 240 and 241.What is ignored in line 
239 is not error, but others.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to