-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update kanister bolckstorage/aws with k10 (#4960)
- Loading branch information
1 parent
e280760
commit 4245488
Showing
5 changed files
with
356 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// +build !unit | ||
|
||
package awsebs | ||
|
||
import ( | ||
"context" | ||
|
||
. "gopkg.in/check.v1" | ||
) | ||
|
||
type KubeTestAWSEBSSuite struct{} | ||
|
||
var _ = Suite(&KubeTestAWSEBSSuite{}) | ||
|
||
func (s KubeTestAWSEBSSuite) TestNodeZones(c *C) { | ||
ctx := context.Background() | ||
zones, err := nodeZones(ctx) | ||
c.Assert(err, IsNil) | ||
c.Assert(zones, Not(HasLen), 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package awsebs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "gopkg.in/check.v1" | ||
) | ||
|
||
// Hook up gocheck into the "go test" runner. | ||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type AWSEBSSuite struct{} | ||
|
||
var _ = Suite(&AWSEBSSuite{}) | ||
|
||
func (s AWSEBSSuite) TestQueryRegionToZones(c *C) { | ||
c.Skip("Only works on AWS") | ||
ctx := context.Background() | ||
region := "us-east-1" | ||
zs, err := queryRegionToZones(ctx, region) | ||
c.Assert(err, IsNil) | ||
c.Assert(zs, DeepEquals, []string{"us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d", "us-east-1e", "us-east-1f"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package awsebs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"github.com/kanisterio/kanister/pkg/kube" | ||
) | ||
|
||
func zoneForVolumeCreateFromSnapshot(ctx context.Context, region string, sourceZone string) (string, error) { | ||
nzs, err := nodeZones(ctx) | ||
if err != nil { | ||
log.Errorf("Ignoring error getting Node availability zones. Error: %+v", err) | ||
} | ||
if len(nzs) != 0 { | ||
var z string | ||
if z, err = zoneFromKnownNodeZones(ctx, region, sourceZone, nzs); err == nil && isZoneValid(z, region) { | ||
return z, nil | ||
} | ||
} | ||
return zoneWithUnknownNodeZones(ctx, region, sourceZone) | ||
} | ||
|
||
func isZoneValid(zone, region string) bool { | ||
if validZones, err := staticRegionToZones(region); err == nil { | ||
for _, z := range validZones { | ||
if zone == z { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func zoneFromKnownNodeZones(ctx context.Context, region string, sourceZone string, nzs map[string]struct{}) (string, error) { | ||
// If the original zone is available, we return that one. | ||
if _, ok := nzs[sourceZone]; ok { | ||
return sourceZone, nil | ||
} | ||
// If there's an available zone with the zone suffix, we use that one. | ||
for nz := range nzs { | ||
if zoneSuffixesMatch(nz, sourceZone) { | ||
return nz, nil | ||
} | ||
} | ||
// If any nodes are available, return an arbitrary one. | ||
// This is relatively random based on go's map iteration. | ||
for nz := range nzs { | ||
return nz, nil | ||
} | ||
// Unreachable | ||
return "", nil | ||
} | ||
|
||
func zoneWithUnknownNodeZones(ctx context.Context, region string, sourceZone string) (string, error) { | ||
// We could not the zones of the nodes, so we return an arbitrary one. | ||
zs, err := regionToZones(ctx, region) | ||
if err != nil || len(zs) == 0 { | ||
// If all else fails, we return the original AZ. | ||
log.Errorf("Using original AZ. region: %s, Error: %+v", region, err) | ||
return sourceZone, nil | ||
} | ||
// We look for a zone with the same suffix. | ||
for _, z := range zs { | ||
if zoneSuffixesMatch(z, sourceZone) { | ||
return z, nil | ||
} | ||
} | ||
// We return an arbitrary zone in the region. | ||
return zs[0], nil | ||
} | ||
|
||
func zoneSuffixesMatch(zone1, zone2 string) bool { | ||
return zone1[len(zone1)-1] == zone2[len(zone2)-1] | ||
} | ||
|
||
const ( | ||
nodeZonesErr = `Failed to get Node availability zones.` | ||
) | ||
|
||
func nodeZones(ctx context.Context) (map[string]struct{}, error) { | ||
cfg, err := kube.LoadConfig() | ||
if err != nil { | ||
return nil, errors.Wrap(err, nodeZonesErr) | ||
} | ||
cli, err := kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
return nil, errors.Wrap(err, nodeZonesErr) | ||
} | ||
ns, err := cli.CoreV1().Nodes().List(metav1.ListOptions{}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, nodeZonesErr) | ||
} | ||
zoneSet := make(map[string]struct{}, len(ns.Items)) | ||
for _, n := range ns.Items { | ||
if v, ok := n.Labels[kube.PVZoneLabelName]; ok { | ||
zoneSet[v] = struct{}{} | ||
} | ||
} | ||
return zoneSet, nil | ||
} |
Oops, something went wrong.