暂停、重启应用功能实现

This commit is contained in:
devad 2024-01-03 15:06:38 +08:00
parent 8edee441d1
commit d4db7753a5
12 changed files with 328 additions and 70 deletions

28
go.mod
View File

@ -8,13 +8,13 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.3
github.com/zeromicro/go-zero v1.6.0
gitlink.org.cn/jcce-pcm/pcm-coordinator v0.1.14-0.20231206063304-ae0e5330582a
gitlink.org.cn/jcce-pcm/pcm-coordinator v0.1.17
gitlink.org.cn/jcce-pcm/utils v0.0.2
google.golang.org/grpc v1.59.0
google.golang.org/grpc v1.60.0
google.golang.org/protobuf v1.31.0
k8s.io/api v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
sigs.k8s.io/yaml v1.4.0
)
@ -46,7 +46,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
@ -64,14 +64,14 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nacos-group/nacos-sdk-go/v2 v2.2.4 // indirect
github.com/openzipkin/zipkin-go v0.4.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.10 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect
go.etcd.io/etcd/client/v3 v3.5.10 // indirect
go.etcd.io/etcd/api/v3 v3.5.11 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.11 // indirect
go.etcd.io/etcd/client/v3 v3.5.11 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
@ -93,9 +93,9 @@ require (
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
@ -104,7 +104,7 @@ require (
gorm.io/driver/mysql v1.5.2 // indirect
gorm.io/gorm v1.25.5 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e // indirect
k8s.io/kube-openapi v0.0.0-20231206194836-bf4651e18aa8 // indirect
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect

64
go.sum
View File

@ -80,8 +80,8 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 h1:6UKoz5ujsI55KNpsJH3UwCq3T8kKbZwNZBNPuTTje8U=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1/go.mod h1:YvJ2f6MplWDhfxiUC3KpyTy76kYUZA4W3pTv/wdKQ9Y=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
@ -146,14 +146,14 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/openzipkin/zipkin-go v0.4.2 h1:zjqfqHjUpPmB3c1GlCvvgsM1G4LkvqQbBDueDOCg/jA=
github.com/openzipkin/zipkin-go v0.4.2/go.mod h1:ZeVkFjuuBiSy13y8vpSDCjMi9GoI3hPpCJSBx/EYFhY=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -198,16 +198,16 @@ github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE
github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
github.com/zeromicro/go-zero v1.6.0 h1:UwSOR1lGZ2g7L0S07PM8RoneAcubtd5x//EfbuNucQ0=
github.com/zeromicro/go-zero v1.6.0/go.mod h1:E9GCFPb0SwsTKFBcFr9UynGvXiDMmfc6fI5F15vqvAQ=
gitlink.org.cn/jcce-pcm/pcm-coordinator v0.1.14-0.20231206063304-ae0e5330582a h1:+aZfuzxBddrVDwgzeRoipt5nrbHtQCtZ2piyvdhwMds=
gitlink.org.cn/jcce-pcm/pcm-coordinator v0.1.14-0.20231206063304-ae0e5330582a/go.mod h1:SUfW2b7f/AvTnwOtxDgTG+YCad9FWzo3p3zLLyurid8=
gitlink.org.cn/jcce-pcm/pcm-coordinator v0.1.17 h1:c/uV63RuQul3QrY1DaLOUNYIx/KPSSGFH8pYwDVX1so=
gitlink.org.cn/jcce-pcm/pcm-coordinator v0.1.17/go.mod h1:0dHxKCTjH3ud1qRQZjE6EqXSs3NTOpiHWTpaip4mrWE=
gitlink.org.cn/jcce-pcm/utils v0.0.2 h1:Stif8W9C9TOCS2hw4g+OlOywDrsVYNrkiyKfBrWkT0w=
gitlink.org.cn/jcce-pcm/utils v0.0.2/go.mod h1:u8PTlBpzUyOlbQJgfSiutq91q/JtrJIQiPNDe4S/pGs=
go.etcd.io/etcd/api/v3 v3.5.10 h1:szRajuUUbLyppkhs9K6BRtjY37l66XQQmw7oZRANE4k=
go.etcd.io/etcd/api/v3 v3.5.10/go.mod h1:TidfmT4Uycad3NM/o25fG3J07odo4GBB9hoxaodFCtI=
go.etcd.io/etcd/client/pkg/v3 v3.5.10 h1:kfYIdQftBnbAq8pUWFXfpuuxFSKzlmM5cSn76JByiT0=
go.etcd.io/etcd/client/pkg/v3 v3.5.10/go.mod h1:DYivfIviIuQ8+/lCq4vcxuseg2P2XbHygkKwFo9fc8U=
go.etcd.io/etcd/client/v3 v3.5.10 h1:W9TXNZ+oB3MCd/8UjxHTWK5J9Nquw9fQBLJd5ne5/Ao=
go.etcd.io/etcd/client/v3 v3.5.10/go.mod h1:RVeBnDz2PUEZqTpgqwAtUd8nAPf5kjyFyND7P1VkOKc=
go.etcd.io/etcd/api/v3 v3.5.11 h1:B54KwXbWDHyD3XYAwprxNzTe7vlhR69LuBgZnMVvS7E=
go.etcd.io/etcd/api/v3 v3.5.11/go.mod h1:Ot+o0SWSyT6uHhA56al1oCED0JImsRiU9Dc26+C2a+4=
go.etcd.io/etcd/client/pkg/v3 v3.5.11 h1:bT2xVspdiCj2910T0V+/KHcVKjkUrCZVtk8J2JF2z1A=
go.etcd.io/etcd/client/pkg/v3 v3.5.11/go.mod h1:seTzl2d9APP8R5Y2hFL3NVlD6qC/dOT+3kvrqPyTas4=
go.etcd.io/etcd/client/v3 v3.5.11 h1:ajWtgoNSZJ1gmS8k+icvPtqsqEav+iUorF7b0qozgUU=
go.etcd.io/etcd/client/v3 v3.5.11/go.mod h1:a6xQUEqFJ8vztO1agJh/KQKOMfFI8og52ZconzcDJwE=
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 h1:D7UpUy2Xc2wsi1Ras6V40q806WM07rqoCWzXu7Sqy+4=
@ -323,14 +323,14 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 h1:W12Pwm4urIbRdGhMEg2NM9O3TWKjNcxQhs46V0ypf/k=
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4/go.mod h1:5RBcpGRxr25RbDzY5w+dmaqpSEvl8Gwl1x2CICf60ic=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 h1:ZcOkrmX74HbKFYnpPY8Qsw93fC29TbJXspYKaBkSXDQ=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4/go.mod h1:k2dtGpRrbsSyKcNPKKI5sstZkrNCZwpU/ns96JoHbGg=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 h1:DC7wcm+i+P1rN3Ff07vL+OndGg5OhNddHyTA+ocPqYE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4/go.mod h1:eJVxU6o+4G1PSczBr85xmyvSNYAKvAYgkub40YGomFM=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 h1:YJ5pD9rF8o9Qtta0Cmy9rdBwkSjrTCT6XTiUQVOtIos=
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0/go.mod h1:l/k7rMz0vFTBPy+tFSGvXEd3z+BcoG1k7EHbqm+YBsY=
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 h1:s1w3X6gQxwrLEpxnLd/qXTVLgQE2yXwaOaoa6IlY/+o=
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0/go.mod h1:CAny0tYF+0/9rmDB9fahA9YLzX3+AEVl1qXbv5hhj6c=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA=
google.golang.org/grpc v1.60.0 h1:6FQAR0kM31P6MRdeluor2w2gPaS4SVNrD/DNTxrQ15k=
google.golang.org/grpc v1.60.0/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
@ -365,16 +365,16 @@ gorm.io/driver/sqlserver v1.4.1/go.mod h1:DJ4P+MeZbc5rvY58PnmN1Lnyvb5gw5NPzGshHD
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
k8s.io/api v0.28.4 h1:8ZBrLjwosLl/NYgv1P7EQLqoO8MGQApnbgH8tu3BMzY=
k8s.io/api v0.28.4/go.mod h1:axWTGrY88s/5YE+JSt4uUi6NMM+gur1en2REMR7IRj0=
k8s.io/apimachinery v0.28.4 h1:zOSJe1mc+GxuMnFzD4Z/U1wst50X28ZNsn5bhgIIao8=
k8s.io/apimachinery v0.28.4/go.mod h1:wI37ncBvfAoswfq626yPTe6Bz1c22L7uaJ8dho83mgg=
k8s.io/client-go v0.28.4 h1:Np5ocjlZcTrkyRJ3+T3PkXDpe4UpatQxj85+xjaD2wY=
k8s.io/client-go v0.28.4/go.mod h1:0VDZFpgoZfelyP5Wqu0/r/TRYcLYuJ2U1KEeoaPa1N4=
k8s.io/api v0.29.0 h1:NiCdQMY1QOp1H8lfRyeEf8eOwV6+0xA6XEE44ohDX2A=
k8s.io/api v0.29.0/go.mod h1:sdVmXoz2Bo/cb77Pxi71IPTSErEW32xa4aXwKH7gfBA=
k8s.io/apimachinery v0.29.0 h1:+ACVktwyicPz0oc6MTMLwa2Pw3ouLAfAon1wPLtG48o=
k8s.io/apimachinery v0.29.0/go.mod h1:eVBxQ/cwiJxH58eK/jd/vAk4mrxmVlnpBH5J2GbMeis=
k8s.io/client-go v0.29.0 h1:KmlDtFcrdUzOYrBhXHgKw5ycWzc3ryPX5mQe0SkG3y8=
k8s.io/client-go v0.29.0/go.mod h1:yLkXH4HKMAywcrD82KMSmfYg2DlE8mepPR4JGSo5n38=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e h1:snPmy96t93RredGRjKfMFt+gvxuVAncqSAyBveJtr4Q=
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/kube-openapi v0.0.0-20231206194836-bf4651e18aa8 h1:vzKzxN5uyJZLY8HL1/OovW7BJefnsBIWt8T7Gjh2boQ=
k8s.io/kube-openapi v0.0.0-20231206194836-bf4651e18aa8/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/utils v0.0.0-20231127182322-b307cd553661 h1:FepOBzJ0GXm8t0su67ln2wAZjbQ6RxQGZDnzuLcrUTI=
k8s.io/utils v0.0.0-20231127182322-b307cd553661/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=

View File

@ -17,3 +17,23 @@ func ApplyList(svc *svc.ServiceContext) {
}
}
func DeleteList(svc *svc.ServiceContext) {
listResp, err := svc.ParticipantRpc.DeleteList(context.Background(), &participantservice.ApplyListReq{})
if err != nil {
return
}
for _, info := range listResp.InfoList {
pkg.Delete(info.YamlString, pkg.KClients[info.ParticipantName].ClientSet, pkg.KClients[info.ParticipantName].DynamicClient)
}
}
func RestartList(svc *svc.ServiceContext) {
listResp, err := svc.ParticipantRpc.RestartList(context.Background(), &participantservice.ApplyListReq{})
if err != nil {
return
}
for _, info := range listResp.InfoList {
pkg.Restart(info.Name, info.Namespace, info.Kind, pkg.KClients[info.ParticipantName].ClientSet, pkg.KClients[info.ParticipantName].DynamicClient)
}
}

View File

@ -26,8 +26,10 @@ func AddCronGroup(svc *svc.ServiceContext) {
SyncProfile(svc)
})
// 从C端同步配置信息
// 从C端同步任务信息
svc.Cron.AddFunc("*/5 * * * * ?", func() {
ApplyList(svc)
DeleteList(svc)
RestartList(svc)
})
}

View File

@ -0,0 +1,52 @@
package logic
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"time"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/kubernetes"
"github.com/zeromicro/go-zero/core/logx"
)
type RestartStatefulSetLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewRestartStatefulSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RestartStatefulSetLogic {
return &RestartStatefulSetLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// RestartStatefulSet 重启sts
func (l *RestartStatefulSetLogic) RestartStatefulSet(in *kubernetes.DeploymentDetailReq) (*kubernetes.Resp, error) {
//实现kubectl rollout restart StatefulSets功能
patchOpt := metav1.PatchOptions{FieldManager: "kubectl-rollout"}
dt := time.Now()
patchInfo := fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"%s"}}}}}`, dt.String())
_, err := l.svcCtx.ClientSet.AppsV1().StatefulSets(in.Namespace).Patch(l.ctx, in.Name, types.StrategicMergePatchType, []byte(patchInfo), patchOpt)
if err != nil {
return &kubernetes.Resp{
Code: "500",
Msg: "failed",
Data: err.Error(),
}, nil
}
return &kubernetes.Resp{
Code: "200",
Msg: "success",
}, nil
}

81
internal/pkg/delete.go Normal file
View File

@ -0,0 +1,81 @@
package pkg
import (
"bytes"
"context"
"github.com/sirupsen/logrus"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
kubernetes2 "k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
"github.com/zeromicro/go-zero/core/logx"
)
func Delete(yamlString string, clientSet *kubernetes2.Clientset, dynamicClient dynamic.Interface) {
d := yaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
dc := clientSet.Discovery()
restMapperRes, err := restmapper.GetAPIGroupResources(dc)
if err != nil {
logx.WithContext(context.TODO()).Errorf("delete %v \n", err)
}
restMapper := restmapper.NewDiscoveryRESTMapper(restMapperRes)
for {
ext := runtime.RawExtension{}
if err := d.Decode(&ext); err != nil {
if err == io.EOF {
break
}
logx.WithContext(context.TODO()).Errorf("delete %v \n", err)
}
obj, gvk, err := unstructured.UnstructuredJSONScheme.Decode(ext.Raw, nil, nil)
if err != nil {
logrus.Error(err)
}
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
logrus.Error(err)
}
// runtime.Object转换为unstructed
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
logrus.Error(err)
}
var unstruct unstructured.Unstructured
unstruct.Object = unstructuredObj
tmpMetadata := unstructuredObj["metadata"].(map[string]interface{})
tmpName := tmpMetadata["name"].(string)
tmpKind := unstructuredObj["kind"].(string)
if unstruct.GetNamespace() == "" {
unstruct.SetNamespace("default")
}
logrus.Info("deleting resource name: " + tmpName + ", kind: " + tmpKind + ", ns: " + unstruct.GetNamespace())
if unstruct.GetNamespace() == "" {
err := dynamicClient.Resource(mapping.Resource).Delete(context.TODO(), tmpName, metav1.DeleteOptions{})
if err != nil {
logrus.Error(err)
}
} else {
err := dynamicClient.Resource(mapping.Resource).Namespace(unstruct.GetNamespace()).Delete(context.TODO(), tmpName, metav1.DeleteOptions{})
if err != nil {
logrus.Error(err)
}
}
}
}

42
internal/pkg/restart.go Normal file
View File

@ -0,0 +1,42 @@
package pkg
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
kubernetes2 "k8s.io/client-go/kubernetes"
"time"
)
func Restart(name, namespace, kind string, clientSet *kubernetes2.Clientset, dynamicClient dynamic.Interface) {
fmt.Println("重启应用成功")
switch kind {
case "Deployment":
//实现kubectl rollout restart StatefulSets功能
patchOpt := metav1.PatchOptions{FieldManager: "kubectl-rollout"}
dt := time.Now()
patchInfo := fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"%s"}}}}}`, dt.String())
_, err := clientSet.AppsV1().Deployments(namespace).Patch(context.Background(), name, types.StrategicMergePatchType, []byte(patchInfo), patchOpt)
if err != nil {
logrus.Error(err)
}
case "StatefulSet":
//实现kubectl rollout restart StatefulSets功能
patchOpt := metav1.PatchOptions{FieldManager: "kubectl-rollout"}
dt := time.Now()
patchInfo := fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"%s"}}}}}`, dt.String())
_, err := clientSet.AppsV1().StatefulSets(namespace).Patch(context.Background(), name, types.StrategicMergePatchType, []byte(patchInfo), patchOpt)
if err != nil {
logrus.Error(err)
}
}
}

View File

@ -122,3 +122,9 @@ func (s *KubernetesServer) GetAppPodsByAppName(ctx context.Context, in *kubernet
l := logic.NewGetAppPodsByAppNameLogic(ctx, s.svcCtx)
return l.GetAppPodsByAppName(in)
}
// 重启sts
func (s *KubernetesServer) RestartStatefulSet(ctx context.Context, in *kubernetes.DeploymentDetailReq) (*kubernetes.Resp, error) {
l := logic.NewRestartStatefulSetLogic(ctx, s.svcCtx)
return l.RestartStatefulSet(in)
}

View File

@ -33926,7 +33926,7 @@ var file_pb_kubernetes_proto_rawDesc = []byte{
0x0a, 0x61, 0x70, 0x69, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x88, 0x01, 0x01, 0x42, 0x07,
0x0a, 0x05, 0x5f, 0x6b, 0x69, 0x6e, 0x64, 0x42, 0x07, 0x0a, 0x05, 0x5f, 0x6e, 0x61, 0x6d, 0x65,
0x42, 0x0d, 0x0a, 0x0b, 0x5f, 0x61, 0x70, 0x69, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x32,
0xde, 0x09, 0x0a, 0x0a, 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x12, 0x38,
0xa7, 0x0a, 0x0a, 0x0a, 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x12, 0x38,
0x0a, 0x09, 0x41, 0x70, 0x70, 0x6c, 0x79, 0x59, 0x61, 0x6d, 0x6c, 0x12, 0x14, 0x2e, 0x6b, 0x75,
0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x2e, 0x41, 0x70, 0x70, 0x6c, 0x79, 0x52, 0x65,
0x71, 0x1a, 0x15, 0x2e, 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x2e, 0x41,
@ -34004,8 +34004,12 @@ var file_pb_kubernetes_proto_rawDesc = []byte{
0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x2e, 0x41, 0x70, 0x70, 0x44, 0x65, 0x74, 0x61,
0x69, 0x6c, 0x52, 0x65, 0x71, 0x1a, 0x19, 0x2e, 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e, 0x65, 0x74,
0x65, 0x73, 0x2e, 0x50, 0x6f, 0x64, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x73, 0x70,
0x42, 0x0d, 0x5a, 0x0b, 0x2f, 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x12, 0x47, 0x0a, 0x12, 0x52, 0x65, 0x73, 0x74, 0x61, 0x72, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65,
0x66, 0x75, 0x6c, 0x53, 0x65, 0x74, 0x12, 0x1f, 0x2e, 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e, 0x65,
0x74, 0x65, 0x73, 0x2e, 0x44, 0x65, 0x70, 0x6c, 0x6f, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x44, 0x65,
0x74, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x71, 0x1a, 0x10, 0x2e, 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e,
0x65, 0x74, 0x65, 0x73, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x42, 0x0d, 0x5a, 0x0b, 0x2f, 0x6b, 0x75,
0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -34924,26 +34928,28 @@ var file_pb_kubernetes_proto_depIdxs = []int32{
19, // 525: kubernetes.kubernetes.ListNamespace:input_type -> kubernetes.NamespaceListReq
22, // 526: kubernetes.kubernetes.GetAppDetail:input_type -> kubernetes.AppDetailReq
22, // 527: kubernetes.kubernetes.getAppPodsByAppName:input_type -> kubernetes.AppDetailReq
4, // 528: kubernetes.kubernetes.ApplyYaml:output_type -> kubernetes.ApplyResp
3, // 529: kubernetes.kubernetes.DeleteYaml:output_type -> kubernetes.Resp
3, // 530: kubernetes.kubernetes.Get:output_type -> kubernetes.Resp
3, // 531: kubernetes.kubernetes.List:output_type -> kubernetes.Resp
7, // 532: kubernetes.kubernetes.PodList:output_type -> kubernetes.ListPodResp
10, // 533: kubernetes.kubernetes.JobDetail:output_type -> kubernetes.JobDetailResp
12, // 534: kubernetes.kubernetes.DeploymentDetail:output_type -> kubernetes.DeploymentDetailResp
3, // 535: kubernetes.kubernetes.PauseDeployment:output_type -> kubernetes.Resp
3, // 536: kubernetes.kubernetes.StartDeployment:output_type -> kubernetes.Resp
3, // 537: kubernetes.kubernetes.RestartDeployment:output_type -> kubernetes.Resp
3, // 538: kubernetes.kubernetes.DeleteDeployment:output_type -> kubernetes.Resp
14, // 539: kubernetes.kubernetes.ListDeployment:output_type -> kubernetes.DeploymentListResp
3, // 540: kubernetes.kubernetes.DelApp:output_type -> kubernetes.Resp
16, // 541: kubernetes.kubernetes.GetAppByAppName:output_type -> kubernetes.AppJsonResp
18, // 542: kubernetes.kubernetes.updateDeploymentReplica:output_type -> kubernetes.DeploymentResp
21, // 543: kubernetes.kubernetes.ListNamespace:output_type -> kubernetes.TenantListResp
23, // 544: kubernetes.kubernetes.GetAppDetail:output_type -> kubernetes.AppDetailResp
26, // 545: kubernetes.kubernetes.getAppPodsByAppName:output_type -> kubernetes.PodDetailResp
528, // [528:546] is the sub-list for method output_type
510, // [510:528] is the sub-list for method input_type
11, // 528: kubernetes.kubernetes.RestartStatefulSet:input_type -> kubernetes.DeploymentDetailReq
4, // 529: kubernetes.kubernetes.ApplyYaml:output_type -> kubernetes.ApplyResp
3, // 530: kubernetes.kubernetes.DeleteYaml:output_type -> kubernetes.Resp
3, // 531: kubernetes.kubernetes.Get:output_type -> kubernetes.Resp
3, // 532: kubernetes.kubernetes.List:output_type -> kubernetes.Resp
7, // 533: kubernetes.kubernetes.PodList:output_type -> kubernetes.ListPodResp
10, // 534: kubernetes.kubernetes.JobDetail:output_type -> kubernetes.JobDetailResp
12, // 535: kubernetes.kubernetes.DeploymentDetail:output_type -> kubernetes.DeploymentDetailResp
3, // 536: kubernetes.kubernetes.PauseDeployment:output_type -> kubernetes.Resp
3, // 537: kubernetes.kubernetes.StartDeployment:output_type -> kubernetes.Resp
3, // 538: kubernetes.kubernetes.RestartDeployment:output_type -> kubernetes.Resp
3, // 539: kubernetes.kubernetes.DeleteDeployment:output_type -> kubernetes.Resp
14, // 540: kubernetes.kubernetes.ListDeployment:output_type -> kubernetes.DeploymentListResp
3, // 541: kubernetes.kubernetes.DelApp:output_type -> kubernetes.Resp
16, // 542: kubernetes.kubernetes.GetAppByAppName:output_type -> kubernetes.AppJsonResp
18, // 543: kubernetes.kubernetes.updateDeploymentReplica:output_type -> kubernetes.DeploymentResp
21, // 544: kubernetes.kubernetes.ListNamespace:output_type -> kubernetes.TenantListResp
23, // 545: kubernetes.kubernetes.GetAppDetail:output_type -> kubernetes.AppDetailResp
26, // 546: kubernetes.kubernetes.getAppPodsByAppName:output_type -> kubernetes.PodDetailResp
3, // 547: kubernetes.kubernetes.RestartStatefulSet:output_type -> kubernetes.Resp
529, // [529:548] is the sub-list for method output_type
510, // [510:529] is the sub-list for method input_type
510, // [510:510] is the sub-list for extension type_name
510, // [510:510] is the sub-list for extension extendee
0, // [0:510] is the sub-list for field type_name

View File

@ -37,6 +37,7 @@ const (
Kubernetes_ListNamespace_FullMethodName = "/kubernetes.kubernetes/ListNamespace"
Kubernetes_GetAppDetail_FullMethodName = "/kubernetes.kubernetes/GetAppDetail"
Kubernetes_GetAppPodsByAppName_FullMethodName = "/kubernetes.kubernetes/getAppPodsByAppName"
Kubernetes_RestartStatefulSet_FullMethodName = "/kubernetes.kubernetes/RestartStatefulSet"
)
// KubernetesClient is the client API for Kubernetes service.
@ -72,6 +73,8 @@ type KubernetesClient interface {
GetAppDetail(ctx context.Context, in *AppDetailReq, opts ...grpc.CallOption) (*AppDetailResp, error)
// 获取应用的Pod
GetAppPodsByAppName(ctx context.Context, in *AppDetailReq, opts ...grpc.CallOption) (*PodDetailResp, error)
// 重启sts
RestartStatefulSet(ctx context.Context, in *DeploymentDetailReq, opts ...grpc.CallOption) (*Resp, error)
}
type kubernetesClient struct {
@ -244,6 +247,15 @@ func (c *kubernetesClient) GetAppPodsByAppName(ctx context.Context, in *AppDetai
return out, nil
}
func (c *kubernetesClient) RestartStatefulSet(ctx context.Context, in *DeploymentDetailReq, opts ...grpc.CallOption) (*Resp, error) {
out := new(Resp)
err := c.cc.Invoke(ctx, Kubernetes_RestartStatefulSet_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// KubernetesServer is the server API for Kubernetes service.
// All implementations must embed UnimplementedKubernetesServer
// for forward compatibility
@ -277,6 +289,8 @@ type KubernetesServer interface {
GetAppDetail(context.Context, *AppDetailReq) (*AppDetailResp, error)
// 获取应用的Pod
GetAppPodsByAppName(context.Context, *AppDetailReq) (*PodDetailResp, error)
// 重启sts
RestartStatefulSet(context.Context, *DeploymentDetailReq) (*Resp, error)
mustEmbedUnimplementedKubernetesServer()
}
@ -338,6 +352,9 @@ func (UnimplementedKubernetesServer) GetAppDetail(context.Context, *AppDetailReq
func (UnimplementedKubernetesServer) GetAppPodsByAppName(context.Context, *AppDetailReq) (*PodDetailResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetAppPodsByAppName not implemented")
}
func (UnimplementedKubernetesServer) RestartStatefulSet(context.Context, *DeploymentDetailReq) (*Resp, error) {
return nil, status.Errorf(codes.Unimplemented, "method RestartStatefulSet not implemented")
}
func (UnimplementedKubernetesServer) mustEmbedUnimplementedKubernetesServer() {}
// UnsafeKubernetesServer may be embedded to opt out of forward compatibility for this service.
@ -675,6 +692,24 @@ func _Kubernetes_GetAppPodsByAppName_Handler(srv interface{}, ctx context.Contex
return interceptor(ctx, in, info, handler)
}
func _Kubernetes_RestartStatefulSet_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DeploymentDetailReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(KubernetesServer).RestartStatefulSet(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Kubernetes_RestartStatefulSet_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(KubernetesServer).RestartStatefulSet(ctx, req.(*DeploymentDetailReq))
}
return interceptor(ctx, in, info, handler)
}
// Kubernetes_ServiceDesc is the grpc.ServiceDesc for Kubernetes service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
@ -754,6 +789,10 @@ var Kubernetes_ServiceDesc = grpc.ServiceDesc{
MethodName: "getAppPodsByAppName",
Handler: _Kubernetes_GetAppPodsByAppName_Handler,
},
{
MethodName: "RestartStatefulSet",
Handler: _Kubernetes_RestartStatefulSet_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "pb/kubernetes.proto",

View File

@ -380,6 +380,8 @@ type (
GetAppDetail(ctx context.Context, in *AppDetailReq, opts ...grpc.CallOption) (*AppDetailResp, error)
// 获取应用的Pod
GetAppPodsByAppName(ctx context.Context, in *AppDetailReq, opts ...grpc.CallOption) (*PodDetailResp, error)
// 重启sts
RestartStatefulSet(ctx context.Context, in *DeploymentDetailReq, opts ...grpc.CallOption) (*Resp, error)
}
defaultKubernetes struct {
@ -493,3 +495,9 @@ func (m *defaultKubernetes) GetAppPodsByAppName(ctx context.Context, in *AppDeta
client := kubernetes.NewKubernetesClient(m.cli.Conn())
return client.GetAppPodsByAppName(ctx, in, opts...)
}
// 重启sts
func (m *defaultKubernetes) RestartStatefulSet(ctx context.Context, in *DeploymentDetailReq, opts ...grpc.CallOption) (*Resp, error) {
client := kubernetes.NewKubernetesClient(m.cli.Conn())
return client.RestartStatefulSet(ctx, in, opts...)
}

View File

@ -188,6 +188,8 @@ service kubernetes {
rpc GetAppDetail(AppDetailReq) returns (AppDetailResp);
//Pod
rpc getAppPodsByAppName(AppDetailReq) returns (PodDetailResp);
// sts
rpc RestartStatefulSet(DeploymentDetailReq) returns (Resp);
}
message Deployment {