Skip to content

Commit

Permalink
feat: make client generic & update handler with better cloudevent con…
Browse files Browse the repository at this point in the history
…text

Signed-off-by: Zufar Dhiyaullhaq <[email protected]>
  • Loading branch information
zufardhiyaulhaq committed Jan 26, 2025
1 parent 5d4c9ed commit fd41ae0
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 13 deletions.
7 changes: 7 additions & 0 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export CLUSTER_NAME=my-cluster
export ISTIO_VERSION=1.23.0
export CONSUMER_HOST=https://my-host.com
export CLIENT_CERT_PATH=/home/user/client-cert.pem
export CLIENT_KEY_PATH=/home/user/client-key.pem
export EVENT_SOURCE="https://gitlab.com/repo/my-repo"
export EVENT_SUBJECT="commiter:zufar.dhiyaulhaq"
21 changes: 21 additions & 0 deletions client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/gopaytech/istio-upgrade-consumer/client

go 1.22.2

require (
github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.15.2
github.com/cloudevents/sdk-go/v2 v2.15.2
github.com/gopaytech/istio-upgrade-proto v0.0.2
github.com/kelseyhightower/envconfig v1.4.0
)

require (
github.com/google/uuid v1.1.1 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.10.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
46 changes: 46 additions & 0 deletions client/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.15.2 h1:FIvfKlS2mcuP0qYY6yzdIU9xdrRd/YMP0bNwFjXd0u8=
github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.15.2/go.mod h1:POsdVp/08Mki0WD9QvvgRRpg9CQ6zhjfRrBoEY8JFS8=
github.com/cloudevents/sdk-go/v2 v2.15.2 h1:54+I5xQEnI73RBhWHxbI1XJcqOFOVJN85vb41+8mHUc=
github.com/cloudevents/sdk-go/v2 v2.15.2/go.mod h1:lL7kSWAE/V8VI4Wh0jbL2v/jvqsm6tjmaQBSvxcv4uE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopaytech/istio-upgrade-proto v0.0.2 h1:Dgo92ZbID/jv1y9J1+LYLo3+wpdwpgMTwZQ7poD2ces=
github.com/gopaytech/istio-upgrade-proto v0.0.2/go.mod h1:Venys1DzOp4CLjQXHBdmMYLxOtohpsTha9OKeXGyEDc=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33 changes: 22 additions & 11 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ import (
)

func main() {
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/v1/upgrade")
settings, err := NewSettings()
if err != nil {
log.Fatalf("failed to load settings: %v", err)
}

url := settings.Host + "/v1/upgrade"
ctx := cloudevents.ContextWithTarget(context.Background(), url)

p, err := cloudevents.NewHTTP()
if err != nil {
log.Fatalf("failed to create protocol: %s", err.Error())
}

// Load the certificate
p.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{loadCertificate()},
},
if settings.ClientCertPath != "" && settings.ClientKeyPath != "" {
p.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{loadCertificate(settings.ClientCertPath, settings.ClientKeyPath)},
InsecureSkipVerify: true,
},
}
}

c, err := cloudevents.NewClient(p, cloudevents.WithTimeNow(), cloudevents.WithUUIDs())
Expand All @@ -33,12 +41,15 @@ func main() {
}

data := &model.Upgrade{
IstioVersion: "1.22.4",
ClusterName: "s-go-sy-primary-gke-01",
IstioVersion: settings.IstioVersion,
ClusterName: settings.ClusterName,
}

e := cloudevents.NewEvent()
e.SetType("upgrade-event")
e.SetSource("testing-client")
e.SetSource(settings.EventSource)
e.SetSubject(settings.EventSubject)

_ = e.SetData(pbcloudevents.ContentTypeProtobuf, data)

res := c.Send(ctx, e)
Expand All @@ -54,8 +65,8 @@ func main() {
}
}

func loadCertificate() tls.Certificate {
cert, err := tls.LoadX509KeyPair("path/to/certificate.crt", "path/to/private.key")
func loadCertificate(certPath, keyPath string) tls.Certificate {
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
log.Fatalf("failed to load certificate: %v", err)
}
Expand Down
27 changes: 27 additions & 0 deletions client/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/kelseyhightower/envconfig"
)

type Settings struct {
ClusterName string `required:"true" envconfig:"CLUSTER_NAME"`
IstioVersion string `required:"true" envconfig:"ISTIO_VERSION"`
Host string `required:"true" envconfig:"CONSUMER_HOST"`
ClientCertPath string `envconfig:"CLIENT_CERT_PATH"`
ClientKeyPath string `envconfig:"CLIENT_KEY_PATH"`

EventSource string `envconfig:"EVENT_SOURCE"`
EventSubject string `envconfig:"EVENT_SUBJECT"`
}

func NewSettings() (Settings, error) {
var settings Settings

err := envconfig.Process("", &settings)
if err != nil {
return settings, err
}

return settings, nil
}
12 changes: 11 additions & 1 deletion services/receiver/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ type UpgradeHandler struct {
func (h *UpgradeHandler) Handle(ctx context.Context, event cloudevents.Event) {
payload := &model.Upgrade{}
if err := event.DataAs(payload); err != nil {
log.Printf("failed to decode protobuf data: %s", err)
log.Printf("failed to decode protobuf data: %s, skipped", err)
return
}

eventType := event.Context.GetType()
if eventType != "upgrade-event" {
log.Printf("received invalid event: %s, skipped", eventType)
return
}
eventSubject := event.Context.GetSubject()
eventSource := event.Context.GetSource()

log.Printf("received event: %s from %s with subject %s", eventType, eventSource, eventSubject)

upgrade := types.Upgrade{
Version: payload.IstioVersion,
ClusterName: payload.ClusterName,
Expand Down
1 change: 0 additions & 1 deletion types/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ func (u Upgrade) ToConfigMapData() map[string]string {
"rollout_restart_date": u.RolloutRestartDate,
}
}

0 comments on commit fd41ae0

Please sign in to comment.