Skip to content

Commit adbbe9e

Browse files
authored
Merge pull request #107 from Altinity/release-0.3.0
Better SQL retries handling logic
2 parents a0bc676 + 3f140ad commit adbbe9e

File tree

20 files changed

+651
-76
lines changed

20 files changed

+651
-76
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
FROM golang:1.11.5 AS builder
44

5+
RUN apt-get update && apt-get install -y -q apt-utils && apt-get install -y -q gettext-base
56
WORKDIR $GOPATH/src/github.com/altinity/clickhouse-operator
67

78
# Reconstruct source tree inside docker

Gopkg.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/users.d/01-clickhouse-operator-user.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<users>
33
<clickhouse_operator>
44
<networks>
5+
<ip>127.0.0.1</ip>
6+
<ip>0.0.0.0/0</ip>
57
<ip>::/0</ip>
68
</networks>
79
<password>clickhouse_operator_password</password>

dev/binary_build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ REPO="github.com/altinity/clickhouse-operator"
1111
VERSION=$(cd ${SRC_ROOT}; cat release)
1212
GIT_SHA=$(cd ${CUR_DIR}; git rev-parse --short HEAD)
1313

14+
# Build clickhouse-operator install .yaml manifest
15+
${SRC_ROOT}/manifests/operator/build-clickhouse-operator-yaml.sh
16+
1417
#CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${CUR_DIR}/clickhouse-operator ${SRC_ROOT}/cmd/clickhouse-operator
1518
CGO_ENABLED=0 go build \
1619
-ldflags "-X ${REPO}/pkg/version.Version=${VERSION} -X ${REPO}/pkg/version.GitSHA=${GIT_SHA}" \

dev/image_build_universal.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ SRC_ROOT="$(realpath ${CUR_DIR}/..)"
1414
DOCKERFILE_DIR="${SRC_ROOT}"
1515
DOCKERFILE="${DOCKERFILE_DIR}/Dockerfile"
1616

17+
# Build clickhouse-operator install .yaml manifest
18+
${SRC_ROOT}/manifests/operator/build-clickhouse-operator-yaml.sh
19+
1720
# Build image with Docker
1821
if [[ "${MINIKUBE}" == "yes" ]]; then
1922
# We'd like to build for minikube

manifests/operator/build-clickhouse-operator-yaml.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ MANIFEST_ROOT=$(realpath ${CUR_DIR}/..)
88

99
CHOPERATOR_IMAGE="${CHOPERATOR_IMAGE}" \
1010
CHOPERATOR_NAMESPACE="${CHOPERATOR_NAMESPACE}" \
11-
${MANIFEST_ROOT}/dev/cat-clickhouse-operator-yaml.sh | tee ${CUR_DIR}/clickhouse-operator-install.yaml
11+
${MANIFEST_ROOT}/dev/cat-clickhouse-operator-yaml.sh > ${CUR_DIR}/clickhouse-operator-install.yaml

manifests/operator/clickhouse-operator-install.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ data:
410410
<users>
411411
<clickhouse_operator>
412412
<networks>
413+
<ip>127.0.0.1</ip>
414+
<ip>0.0.0.0/0</ip>
413415
<ip>::/0</ip>
414416
</networks>
415417
<password>clickhouse_operator_password</password>

pkg/apis/metrics/fetcher.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package metrics
1616

1717
import (
18+
"github.com/MakeNowJust/heredoc"
19+
1820
"github.com/altinity/clickhouse-operator/pkg/model/clickhouse"
1921
)
2022

@@ -79,7 +81,7 @@ func (f *Fetcher) newConn() *clickhouse.Conn {
7981
// data is a concealed output
8082
func (f *Fetcher) clickHouseQueryMetrics(data *[][]string) error {
8183
conn := f.newConn()
82-
if rows, err := conn.Query(queryMetricsSQL); err != nil {
84+
if rows, err := conn.Query(heredoc.Doc(queryMetricsSQL)); err != nil {
8385
return err
8486
} else {
8587
for rows.Next() {
@@ -98,7 +100,7 @@ func (f *Fetcher) clickHouseQueryMetrics(data *[][]string) error {
98100
// data is a concealed output
99101
func (f *Fetcher) clickHouseQueryTableSizes(data *[][]string) error {
100102
conn := f.newConn()
101-
if rows, err := conn.Query(queryTableSizesSQL); err != nil {
103+
if rows, err := conn.Query(heredoc.Doc(queryTableSizesSQL)); err != nil {
102104
return err
103105
} else {
104106
for rows.Next() {

pkg/controller/chi/controller.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (c *Controller) AddEventHandlers(
248248
glog.V(1).Infof("endpointsInformer UpdateFunc(%s/%s) IP ASSIGNED %v", newEndpoints.Namespace, newEndpoints.Name, newEndpoints.Subsets)
249249
if chi, err := c.createChiFromObjectMeta(&newEndpoints.ObjectMeta); err == nil {
250250
glog.V(1).Infof("endpointsInformer UpdateFunc(%s/%s) flushing DNS for CHI %s", newEndpoints.Namespace, newEndpoints.Name, chi.Name)
251-
c.schemer.ChiDropDnsCache(chi)
251+
_ = c.schemer.ChiDropDnsCache(chi)
252252
} else {
253253
glog.V(1).Infof("endpointsInformer UpdateFunc(%s/%s) unable to find CHI by %v", newEndpoints.Namespace, newEndpoints.Name, newEndpoints.ObjectMeta.Labels)
254254
}
@@ -361,15 +361,8 @@ func (c *Controller) Run(ctx context.Context, threadiness int) {
361361
return
362362
}
363363

364-
// Label operator's Pod with version label
365-
podname, ok1 := c.runtimeParams["OPERATOR_POD_NAME"]
366-
namespace, ok2 := c.runtimeParams["OPERATOR_POD_NAMESPACE"]
367-
if ok1 && ok2 {
368-
if pod, err := c.podLister.Pods(namespace).Get(podname); err == nil {
369-
pod.Labels["version"] = c.version
370-
c.kubeClient.CoreV1().Pods(namespace).Update(pod)
371-
}
372-
}
364+
// Label controller runtime objects with proper labels
365+
c.labelMyObjectsTree()
373366

374367
glog.V(1).Info("ClickHouseInstallation controller: starting workers")
375368
for i := 0; i < threadiness; i++ {

pkg/controller/chi/labeler.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package chi
16+
17+
import (
18+
"github.com/golang/glog"
19+
"k8s.io/apimachinery/pkg/apis/meta/v1"
20+
)
21+
22+
func (c *Controller) labelMyObjectsTree() {
23+
24+
// Operator is running in the Pod. Label Pod
25+
// Pod is owned by ReplicaSet. Label ReplicaSet also.
26+
// ReplicaSet is owned by Deployment. Label Deployment also.
27+
// Deployment is not owned so far.
28+
//
29+
// Excerpt from Pod's yaml
30+
// metadata:
31+
// ownerReferences:
32+
// - apiVersion: apps/v1
33+
// blockOwnerDeletion: true
34+
// controller: true
35+
// kind: ReplicaSet
36+
// name: clickhouse-operator-79bf98f9b8
37+
// uid: a276f30c-83ae-11e9-b92d-0208b778ea1a
38+
//
39+
// Excerpt from ReplicaSet's yaml
40+
// metadata:
41+
// ownerReferences:
42+
// - apiVersion: apps/v1
43+
// blockOwnerDeletion: true
44+
// controller: true
45+
// kind: Deployment
46+
// name: clickhouse-operator
47+
// uid: a275a8a0-83ae-11e9-b92d-0208b778ea1a
48+
49+
// Label operator's Pod with version label
50+
podName, ok1 := c.runtimeParams["OPERATOR_POD_NAME"]
51+
namespace, ok2 := c.runtimeParams["OPERATOR_POD_NAMESPACE"]
52+
53+
if !ok1 || !ok2 {
54+
glog.V(1).Info("ERROR fetch Pod name out of %s/%s", namespace, podName)
55+
return
56+
}
57+
58+
// Pod namespaced name found, fetch it
59+
pod, err := c.podLister.Pods(namespace).Get(podName)
60+
if err != nil {
61+
glog.V(1).Info("ERROR get Pod %s/%s", namespace, podName)
62+
return
63+
}
64+
65+
// Put label on the Pod
66+
pod.Labels["version"] = c.version
67+
if _, err := c.kubeClient.CoreV1().Pods(namespace).Update(pod); err != nil {
68+
glog.V(1).Info("ERROR put label on Pod %s/%s", namespace, podName)
69+
}
70+
71+
// Find parent ReplicaSet
72+
replicaSetName := ""
73+
for i := range pod.OwnerReferences {
74+
owner := &pod.OwnerReferences[i]
75+
if owner.Kind == "ReplicaSet" {
76+
// ReplicaSet found
77+
replicaSetName = owner.Name
78+
break
79+
}
80+
}
81+
82+
if replicaSetName == "" {
83+
// ReplicaSet not found
84+
glog.V(1).Info("ERROR ReplicaSet for Pod %s/%s not found", namespace, podName)
85+
return
86+
}
87+
88+
// ReplicaSet namespaced name found, fetch it
89+
replicaSet, err := c.kubeClient.AppsV1().ReplicaSets(namespace).Get(replicaSetName, v1.GetOptions{})
90+
if err != nil {
91+
glog.V(1).Info("ERROR get ReplicaSet %s/%s", namespace, replicaSetName)
92+
return
93+
}
94+
95+
// Put label on the ReplicaSet
96+
replicaSet.Labels["version"] = c.version
97+
if _, err := c.kubeClient.AppsV1().ReplicaSets(namespace).Update(replicaSet); err != nil {
98+
glog.V(1).Info("ERROR put label on ReplicaSet %s/%s", namespace, replicaSetName)
99+
}
100+
101+
// Find parent Deployment
102+
deploymentName := ""
103+
for i := range replicaSet.OwnerReferences {
104+
owner := &replicaSet.OwnerReferences[i]
105+
if owner.Kind == "Deployment" {
106+
// Deployment found
107+
deploymentName = owner.Name
108+
break
109+
}
110+
}
111+
112+
if deploymentName == "" {
113+
// Deployment not found
114+
glog.V(1).Info("ERROR Deployment for %s Pod %s ReplicaSet %s not found", namespace, podName, replicaSetName)
115+
return
116+
}
117+
118+
// Deployment namespaced name found, fetch it
119+
deployment, err := c.kubeClient.AppsV1().Deployments(namespace).Get(deploymentName, v1.GetOptions{})
120+
if err != nil {
121+
glog.V(1).Info("ERROR get Deployment %s/%s", namespace, deploymentName)
122+
return
123+
}
124+
125+
// Put label on the Deployment
126+
deployment.Labels["version"] = c.version
127+
if _, err := c.kubeClient.AppsV1().Deployments(namespace).Update(deployment); err != nil {
128+
glog.V(1).Info("ERROR put label on Deployment %s/%s", namespace, deploymentName)
129+
}
130+
}

0 commit comments

Comments
 (0)