Skip to content

Commit

Permalink
Merge pull request #310 from MrHohn/composite-backend-service-beta
Browse files Browse the repository at this point in the history
 Add beta backend service support to composite type
  • Loading branch information
nicksardo authored Jun 6, 2018
2 parents fda3d63 + 0e422f5 commit 169e8d6
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 26 deletions.
82 changes: 58 additions & 24 deletions pkg/backends/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/golang/glog"

computealpha "google.golang.org/api/compute/v0.alpha"
computebeta "google.golang.org/api/compute/v0.beta"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
Expand All @@ -32,57 +33,75 @@ import (
// TODO(rramkumar): All code in this file should ideally be generated.

func createBackendService(be *BackendService, cloud *gce.GCECloud) error {
if be.Version == meta.VersionAlpha {
switch be.Version {
case meta.VersionAlpha:
alpha, err := be.toAlpha()
if err != nil {
return err
}
glog.V(3).Infof("Creating alpha backend service %v", alpha.Name)
return cloud.CreateAlphaGlobalBackendService(alpha)
case meta.VersionBeta:
beta, err := be.toBeta()
if err != nil {
return err
}
glog.V(3).Infof("Creating beta backend service %v", beta.Name)
return cloud.CreateBetaGlobalBackendService(beta)
default:
ga, err := be.toGA()
if err != nil {
return err
}
glog.V(3).Infof("Creating ga backend service %v", ga.Name)
return cloud.CreateGlobalBackendService(ga)
}
ga, err := be.toGA()
if err != nil {
return err
}
glog.V(3).Infof("Creating ga backend service %v", ga.Name)
return cloud.CreateGlobalBackendService(ga)
}

func updateBackendService(be *BackendService, cloud *gce.GCECloud) error {
if be.Version == meta.VersionAlpha {
switch be.Version {
case meta.VersionAlpha:
alpha, err := be.toAlpha()
if err != nil {
return err
}
glog.V(3).Infof("Updating alpha backend service %v", alpha.Name)
return cloud.UpdateAlphaGlobalBackendService(alpha)
case meta.VersionBeta:
beta, err := be.toBeta()
if err != nil {
return err
}
glog.V(3).Infof("Updating beta backend service %v", beta.Name)
return cloud.UpdateBetaGlobalBackendService(beta)
default:
ga, err := be.toGA()
if err != nil {
return err
}
glog.V(3).Infof("Updating ga backend service %v", ga.Name)
return cloud.UpdateGlobalBackendService(ga)
}
ga, err := be.toGA()
if err != nil {
return err
}
glog.V(3).Infof("Updating ga backend service %v", ga.Name)
return cloud.UpdateGlobalBackendService(ga)
}

func getBackendService(name string, version meta.Version, cloud *gce.GCECloud) (*BackendService, error) {
var gceObj interface{}
var err error
if version == meta.VersionAlpha {
switch version {
case meta.VersionAlpha:
gceObj, err = cloud.GetAlphaGlobalBackendService(name)
if err != nil {
return nil, err
}
} else {
case meta.VersionBeta:
gceObj, err = cloud.GetBetaGlobalBackendService(name)
default:
gceObj, err = cloud.GetGlobalBackendService(name)
if err != nil {
return nil, err
}
}
if err != nil {
return nil, err
}
return toBackendService(gceObj)
}

// toBackendService converts a compute alpha or GA
// toBackendService converts a compute alpha, beta or GA
// BackendService into our composite type.
func toBackendService(obj interface{}) (*BackendService, error) {
be := &BackendService{}
Expand All @@ -98,7 +117,7 @@ func toBackendService(obj interface{}) (*BackendService, error) {
}

// BackendService is a composite type which embeds the
// structure of both the compute alpha and GA Backend Service.
// structure of all the compute alpha, beta and GA Backend Service.
type BackendService struct {
// Version keeps track of the intended compute version for this BackendService.
// Note that the compute API's do not contain this field. It is for our
Expand Down Expand Up @@ -224,6 +243,21 @@ func (be *BackendService) toAlpha() (*computealpha.BackendService, error) {
return alpha, nil
}

// toBeta converts our composite type into an beta type.
// This beta type can be used in GCE API calls.
func (be *BackendService) toBeta() (*computebeta.BackendService, error) {
bytes, err := json.Marshal(be)
if err != nil {
return nil, fmt.Errorf("error marshalling BackendService to JSON: %v", err)
}
beta := &computebeta.BackendService{}
err = json.Unmarshal(bytes, beta)
if err != nil {
return nil, fmt.Errorf("error unmarshalling BackendService JSON to compute beta type: %v", err)
}
return beta, nil
}

// toGA converts our composite type into a GA type.
// This GA type can be used in GCE API calls.
func (be *BackendService) toGA() (*compute.BackendService, error) {
Expand Down
19 changes: 19 additions & 0 deletions pkg/backends/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/kr/pretty"
computealpha "google.golang.org/api/compute/v0.alpha"
computebeta "google.golang.org/api/compute/v0.beta"
compute "google.golang.org/api/compute/v1"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/meta"
)
Expand Down Expand Up @@ -121,6 +122,10 @@ func TestToBackendService(t *testing.T) {
computealpha.BackendService{Protocol: "HTTP2"},
&BackendService{Protocol: "HTTP2"},
},
{
computebeta.BackendService{Protocol: "HTTP"},
&BackendService{Protocol: "HTTP"},
},
{
compute.BackendService{Protocol: "HTTP"},
&BackendService{Protocol: "HTTP"},
Expand Down Expand Up @@ -149,6 +154,20 @@ func TestToAlpha(t *testing.T) {
}
}

func TestToBeta(t *testing.T) {
composite := BackendService{
Version: meta.VersionBeta,
Protocol: "HTTP",
}
beta, err := composite.toBeta()
if err != nil {
t.Fatalf("composite.toBeta() =_, %v; want _, nil", err)
}
if beta.Protocol != "HTTP" {
t.Errorf("beta.Protocol = %q, want HTTP", beta.Protocol)
}
}

func TestToGA(t *testing.T) {
composite := BackendService{
Version: meta.VersionGA,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 169e8d6

Please sign in to comment.