From a1a57c9d7987b7b3b3eba3b4602d5cb56b3e9706 Mon Sep 17 00:00:00 2001 From: "Dina.Yakovlev" Date: Wed, 15 Dec 2021 10:20:21 +0200 Subject: [PATCH] Feat: Add server to get redis cluster info (#128) * fix: changes to make telepresence work * fix: change k8s version * feat: add server and router, without module * feat: add server to get cluster info * fix according to review comments Co-authored-by: Dina Yakovlev --- Dockerfile | 2 + config/manager/base/manager.yaml | 4 ++ controllers/configmap_controller.go | 2 +- controllers/rediscluster.go | 11 +++ controllers/rediscluster_controller.go | 3 + data/clusterData.go | 32 +++++++++ go.mod | 1 + go.sum | 42 +++++++++++ hack/dev.Dockerfile | 4 ++ helm/templates/redisoperator/service.yaml | 6 +- helm/values.yaml | 9 ++- main.go | 7 ++ server/module.go | 85 +++++++++++++++++++++++ server/router.go | 10 +++ server/server.go | 15 ++++ 15 files changed, 229 insertions(+), 4 deletions(-) create mode 100644 data/clusterData.go create mode 100644 server/module.go create mode 100644 server/router.go create mode 100644 server/server.go diff --git a/Dockerfile b/Dockerfile index c2a20b91..fb81195f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,6 +27,8 @@ RUN go mod download COPY main.go main.go COPY api/ api/ COPY controllers/ controllers/ +COPY server/ server/ +COPY data/ data/ # Build RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go diff --git a/config/manager/base/manager.yaml b/config/manager/base/manager.yaml index adab0346..761aa3e2 100644 --- a/config/manager/base/manager.yaml +++ b/config/manager/base/manager.yaml @@ -36,6 +36,10 @@ spec: value: "admin" - name: REDISCLI_AUTH value: "adminpass" + - name: CLUSTER_VIEW_FILE + value: "cluster_data" + - name: CLUSTER_STATE_FILE + value: "cluster_state" volumeMounts: - name: redis-operator-config mountPath: "/usr/local/etc/" diff --git a/controllers/configmap_controller.go b/controllers/configmap_controller.go index f896073f..08b920fd 100644 --- a/controllers/configmap_controller.go +++ b/controllers/configmap_controller.go @@ -57,7 +57,7 @@ const ACLFilePropagationDuration time.Duration = time.Millisecond * 5000 // Defines the time it takes for Redis to load the new config const ACLFileLoadDuration time.Duration = time.Millisecond * 500 const redisConfigLabelKey string = "redis-cluster" -const handleACLConfigErrorMessage = "Failed to handle ACL configuation" +const handleACLConfigErrorMessage = "Failed to handle ACL configuration" const operatorConfigLabelKey string = "redis-operator" func (r *RedisConfigReconciler) syncConfig(latestConfigHash string, redisPods ...corev1.Pod) error { diff --git a/controllers/rediscluster.go b/controllers/rediscluster.go index 01d80313..3f68d910 100644 --- a/controllers/rediscluster.go +++ b/controllers/rediscluster.go @@ -1,6 +1,7 @@ package controllers import ( + "encoding/json" "fmt" "reflect" "sort" @@ -15,6 +16,7 @@ import ( dbv1 "github.com/PayU/redis-operator/api/v1" rediscli "github.com/PayU/redis-operator/controllers/rediscli" + clusterData "github.com/PayU/redis-operator/data" ) var EMPTY struct{} @@ -492,6 +494,8 @@ func (r *RedisClusterReconciler) forgetLostNodes(redisCluster *dbv1.RedisCluster for id := range lostNodeIDSet { r.forgetNode(healthyNodeIPs, id) } + data, _ := json.MarshalIndent(clusterView, "", "") + clusterData.SaveRedisClusterView(data) return nil } @@ -716,6 +720,8 @@ func (r *RedisClusterReconciler) recoverCluster(redisCluster *dbv1.RedisCluster) if err != nil || !complete { return errors.Errorf("Cluster recovery not complete") } + data, _ := json.MarshalIndent(clusterView, "", "") + clusterData.SaveRedisClusterView(data) return nil } @@ -816,6 +822,8 @@ func (r *RedisClusterReconciler) updateCluster(redisCluster *dbv1.RedisCluster) if err = r.cleanupNodeList(clusterView.HealthyNodeIPs()); err != nil { return err } + data, _ := json.MarshalIndent(clusterView, "", "") + clusterData.SaveRedisClusterView(data) return nil } @@ -1089,5 +1097,8 @@ func (r *RedisClusterReconciler) isClusterComplete(redisCluster *dbv1.RedisClust } } } + data, _ := json.MarshalIndent(clusterView, "", "") + clusterData.SaveRedisClusterView(data) + return true, nil } diff --git a/controllers/rediscluster_controller.go b/controllers/rediscluster_controller.go index 2a887b25..5ea7d629 100644 --- a/controllers/rediscluster_controller.go +++ b/controllers/rediscluster_controller.go @@ -31,6 +31,7 @@ import ( corev1 "k8s.io/api/core/v1" "github.com/PayU/redis-operator/controllers/rediscli" + clusterData "github.com/PayU/redis-operator/data" ) const ( @@ -175,6 +176,8 @@ func (r *RedisClusterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error break } + clusterData.SaveRedisClusterState(string(r.State)) + if err != nil { r.Log.Error(err, "Handling error") } diff --git a/data/clusterData.go b/data/clusterData.go new file mode 100644 index 00000000..62a371a6 --- /dev/null +++ b/data/clusterData.go @@ -0,0 +1,32 @@ +package data + +import ( + "io/ioutil" + "os" +) + +func SaveRedisClusterView(data []byte) { + fileName := os.Getenv("CLUSTER_VIEW_FILE") + + _ = ioutil.WriteFile(fileName, data, 0644) +} + +func GetClusterView() ([]byte, error) { + fileName := os.Getenv("CLUSTER_VIEW_FILE") + + return ioutil.ReadFile(fileName) +} + +func SaveRedisClusterState(s string) { + fileName := os.Getenv("CLUSTER_STATE_FILE") + _ = ioutil.WriteFile(fileName, []byte(s), 0644) +} + +func GetRedisClusterState() string { + fileName := os.Getenv("CLUSTER_STATE_FILE") + byteValue, err := ioutil.ReadFile(fileName) + if err != nil { + return "NotExists" + } + return string(byteValue) +} diff --git a/go.mod b/go.mod index 6631f000..6e602ce4 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.15 require ( github.com/go-logr/logr v0.1.0 github.com/go-test/deep v1.0.7 + github.com/labstack/echo/v4 v4.6.1 github.com/pkg/errors v0.9.1 golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e gopkg.in/yaml.v2 v2.3.0 diff --git a/go.sum b/go.sum index 4c09a129..ba23fe3d 100644 --- a/go.sum +++ b/go.sum @@ -147,6 +147,7 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef h1:veQD95Isof8w9/WXiA+pa3tz3fJXkt5B7QaRBrM62gk= @@ -227,6 +228,12 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/labstack/echo v1.4.4 h1:1bEiBNeGSUKxcPDGfZ/7IgdhJJZx8wV/pICJh4W2NJI= +github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= +github.com/labstack/echo/v4 v4.6.1 h1:OMVsrnNFzYlGSdaiYGHbgWQnr+JM7NG+B9suCPie14M= +github.com/labstack/echo/v4 v4.6.1/go.mod h1:RnjgMWNDB9g/HucVWhQYNQP9PvbYf6adqftqryo7s9k= +github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= +github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc= @@ -239,7 +246,15 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM= github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -328,6 +343,11 @@ github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhV github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= +github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1/go.mod h1:QcJo0QPSfTONNIgpN5RA8prR7fF8nkF6cTWTcNerRO8= @@ -354,6 +374,8 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190617133340-57b3e21c3d56/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975 h1:/Tl7pH94bvbAAHBdZJT947M/+gp0+CqQXDtMRC0fseo= golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -378,6 +400,9 @@ golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210913180222-943fd674d43e h1:+b/22bPvDYt4NPDcy4xAGCmON713ONAWFeY3Z7I3tR8= +golang.org/x/net v0.0.0-20210913180222-943fd674d43e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= @@ -398,28 +423,45 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190209173611-3b5209105503/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 h1:xrCZDmdtoloIiooiA9q0OQb9r8HejIHYoHGhGCe1pGg= +golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE= +golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181011042414-1f849cf54d09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/hack/dev.Dockerfile b/hack/dev.Dockerfile index 4c44e680..9344ac43 100644 --- a/hack/dev.Dockerfile +++ b/hack/dev.Dockerfile @@ -32,6 +32,8 @@ ARG ENABLE_LEADER_ELECTION_ARG="true" ARG DEVMODE_ARG="true" ARG REDIS_USERNAME_ARG="admin" ARG REDISAUTH_CLI_ARG="adminpass" +ARG CLUSTER_VIEW_FILE_ARG="cluster_data" +ARG CLUSTER_STATE_FILE_ARG="cluster_state" ENV NAMESPACE_ENV=${NAMESPACE_ARG} ENV METRICS_ADDR_ENV=${METRICS_ADDR_ARG} @@ -39,6 +41,8 @@ ENV ENABLE_LEADER_ELECTION_ENV=${ENABLE_LEADER_ELECTION_ARG} ENV DEVMODE_ENV=${DEVMODE_ARG} ENV REDIS_USERNAME=${REDIS_USERNAME_ARG} ENV REDISAUTH_CLI=${REDISAUTH_CLI_ARG} +ENV CLUSTER_VIEW_FILE=${CLUSTER_STATCLUSTER_VIEW_FILE_ARGE_FILE_ARG} +ENV CLUSTER_STATE_FILE=${CLUSTER_STATE_FILE_ARG} ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on diff --git a/helm/templates/redisoperator/service.yaml b/helm/templates/redisoperator/service.yaml index a65f644c..588516cf 100644 --- a/helm/templates/redisoperator/service.yaml +++ b/helm/templates/redisoperator/service.yaml @@ -5,8 +5,12 @@ metadata: namespace: {{ .Values.redisOperator.namespace }} spec: ports: + - port: 8080 + targetPort: 8080 + name: query - port: 9808 targetPort: 9808 + name: metrics selector: control-plane: controller-manager - redis-operator: redis-operator \ No newline at end of file + redis-operator: redis-operator diff --git a/helm/values.yaml b/helm/values.yaml index 96dad0bb..aa02ba17 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -7,7 +7,7 @@ global: redisOperator: enabled: true namespace: default - managerReplicas: 2 # Number of manger pods the Deployment will create + managerReplicas: 1 # Number of manger pods the Deployment will create configFile: "configfiles/operator.conf" labels: control-plane: "controller-manager" @@ -31,12 +31,17 @@ redisOperator: cpu: 100m memory: 256Mi ports: - - containerPort: 9808 + - containerPort: 8080 + name: query env: - name: REDIS_USERNAME value: "admin" - name: REDISCLI_AUTH value: "adminpass" + - name: CLUSTER_VIEW_FILE + value: "cluster_data" + - name: CLUSTER_STATE_FILE + value: "cluster_state" command: - "./manager" args: diff --git a/main.go b/main.go index a8bd48b5..6bfe8f7c 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( dbv1 "github.com/PayU/redis-operator/api/v1" "github.com/PayU/redis-operator/controllers" "github.com/PayU/redis-operator/controllers/rediscli" + "github.com/PayU/redis-operator/server" "github.com/go-logr/logr" // +kubebuilder:scaffold:imports ) @@ -39,6 +40,12 @@ func getRedisCLI(log *logr.Logger) *rediscli.RedisCLI { } func main() { + go server.StartServer() + + startManager() +} + +func startManager() { var metricsAddr, namespace, enableLeaderElection, devmode string flag.StringVar(&metricsAddr, "metrics-addr", "0.0.0.0:9808", "The address the metric endpoint binds to.") diff --git a/server/module.go b/server/module.go new file mode 100644 index 00000000..182e9526 --- /dev/null +++ b/server/module.go @@ -0,0 +1,85 @@ +package server + +import ( + "encoding/json" + "net/http" + + . "github.com/PayU/redis-operator/controllers" + clusterData "github.com/PayU/redis-operator/data" + "github.com/labstack/echo/v4" + v1 "k8s.io/api/core/v1" +) + +type ResponseRedisClusterView struct { + State string + Nodes []ResponseLeaderNode +} + +type ResponseLeaderNode struct { + PodIp string + NodeNumber string + Failed bool + Terminating bool + Followers []ResponseFollowerNode +} + +type ResponseFollowerNode struct { + PodIp string + NodeNumber string + LeaderNumber string + Failed bool + Terminating bool +} + +func clusterInfo(c echo.Context) error { + byteValue, err := clusterData.GetClusterView() + if err != nil { + return c.String(http.StatusNotFound, "Cluster info not available") + } + + var result RedisClusterView + json.Unmarshal([]byte(byteValue), &result) + + s := clusterData.GetRedisClusterState() + ResponseRedisClusterView := ResponseRedisClusterView{ + State: s, + Nodes: make([]ResponseLeaderNode, len(result)), + } + + for i, leaderNode := range result { + ip := getIP(leaderNode.Pod) + + ResponseRedisClusterView.Nodes[i] = ResponseLeaderNode{ + Followers: make([]ResponseFollowerNode, len(leaderNode.Followers)), + PodIp: ip, + NodeNumber: leaderNode.NodeNumber, + Failed: leaderNode.Failed, + Terminating: leaderNode.Terminating, + } + for j, follower := range leaderNode.Followers { + followerIp := getIP(follower.Pod) + ResponseRedisClusterView.Nodes[i].Followers[j] = ResponseFollowerNode{ + PodIp: followerIp, + NodeNumber: follower.NodeNumber, + LeaderNumber: follower.LeaderNumber, + Failed: follower.Failed, + Terminating: leaderNode.Terminating, + } + } + } + + return c.JSON(http.StatusOK, ResponseRedisClusterView) +} + +func getIP(pod *v1.Pod) string { + if pod == nil { + return "" + } else { + return pod.Status.PodIP + } +} + +func clusterState(c echo.Context) error { + s := clusterData.GetRedisClusterState() + return c.String(http.StatusOK, s) +} diff --git a/server/router.go b/server/router.go new file mode 100644 index 00000000..767c36f9 --- /dev/null +++ b/server/router.go @@ -0,0 +1,10 @@ +package server + +import ( + "github.com/labstack/echo/v4" +) + +func Register(e *echo.Echo) { + e.GET("/state", clusterState) + e.GET("/info", clusterInfo) +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 00000000..12b85fcb --- /dev/null +++ b/server/server.go @@ -0,0 +1,15 @@ +package server + +import ( + "github.com/labstack/echo/v4" +) + +func StartServer() { + e := echo.New() + + // Routes + Register(e) + + // Start server + go e.Logger.Fatal(e.Start("0.0.0.0:8080")) +}