diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e5f0cf97e0..524f58e992 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,13 +12,13 @@ jobs: lint: runs-on: 'windows-2019' steps: - - uses: actions/checkout@v2 - - uses: actions/setup-go@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION }} - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v3 with: version: v1.48 args: --timeout=5m @@ -27,8 +27,8 @@ jobs: test: runs-on: 'windows-2019' steps: - - uses: actions/checkout@v2 - - uses: actions/setup-go@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION }} @@ -59,8 +59,8 @@ jobs: build: runs-on: 'windows-2019' steps: - - uses: actions/checkout@v2 - - uses: actions/setup-go@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION }} diff --git a/hcn/hcn.go b/hcn/hcn.go index eefd88d856..a59202409d 100644 --- a/hcn/hcn.go +++ b/hcn/hcn.go @@ -264,6 +264,18 @@ func SetPolicySupported() error { return platformDoesNotSupportError("SetPolicy") } +// ModifyLoadbalancerSupported returns an error if the HCN version does not support ModifyLoadbalancer. +func ModifyLoadbalancerSupported() error { + supported, err := GetCachedSupportedFeatures() + if err != nil { + return err + } + if supported.ModifyLoadbalancer { + return nil + } + return platformDoesNotSupportError("ModifyLoadbalancer") +} + // VxlanPortSupported returns an error if the HCN version does not support configuring the VXLAN TCP port. func VxlanPortSupported() error { supported, err := GetCachedSupportedFeatures() diff --git a/hcn/hcnerrors.go b/hcn/hcnerrors.go index ad30d320d9..c8cd009749 100644 --- a/hcn/hcnerrors.go +++ b/hcn/hcnerrors.go @@ -10,6 +10,7 @@ import ( "github.com/Microsoft/hcsshim/internal/hcserror" "github.com/Microsoft/hcsshim/internal/interop" "github.com/sirupsen/logrus" + "golang.org/x/sys/windows" ) var ( @@ -50,6 +51,7 @@ type ErrorCode uint32 const ( ERROR_NOT_FOUND = 0x490 HCN_E_PORT_ALREADY_EXISTS ErrorCode = 0x803b0013 + HCN_E_NOTIMPL ErrorCode = ErrorCode(windows.E_NOTIMPL) ) type HcnError struct { @@ -77,6 +79,10 @@ func IsPortAlreadyExistsError(err error) bool { return CheckErrorWithCode(err, HCN_E_PORT_ALREADY_EXISTS) } +func IsNotImplemented(err error) bool { + return CheckErrorWithCode(err, HCN_E_NOTIMPL) +} + func new(hr error, title string, rest string) error { err := &HcnError{} hcsError := hcserror.New(hr, title, rest) diff --git a/hcn/hcnglobals.go b/hcn/hcnglobals.go index d03c48736d..ac9f6ad68e 100644 --- a/hcn/hcnglobals.go +++ b/hcn/hcnglobals.go @@ -76,6 +76,17 @@ var ( //HNS 14.0 allows for TierAcl Policy support TierAclPolicyVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 14, Minor: 0}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} + + //HNS 15.0 allows for NetworkACL Policy support + NetworkACLPolicyVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 15, Minor: 0}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} + + //HNS 15.0 allows for NestedIpSet support + NestedIpSetVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 15, Minor: 0}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} + + //HNS 15.1 allows support for DisableHostPort flag. + DisableHostPortVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 15, Minor: 1}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} + // HNS 15.4 allows for Modify Loadbalancer support + ModifyLoadbalancerVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 15, Minor: 4}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} ) // GetGlobals returns the global properties of the HCN Service. diff --git a/hcn/hcnloadbalancer.go b/hcn/hcnloadbalancer.go index 1b434b07b3..46f1104544 100644 --- a/hcn/hcnloadbalancer.go +++ b/hcn/hcnloadbalancer.go @@ -28,14 +28,14 @@ type HostComputeLoadBalancer struct { Flags LoadBalancerFlags `json:",omitempty"` // 0: None, 1: EnableDirectServerReturn } -//LoadBalancerFlags modify settings for a loadbalancer. +// LoadBalancerFlags modify settings for a loadbalancer. type LoadBalancerFlags uint32 var ( // LoadBalancerFlagsNone is the default. LoadBalancerFlagsNone LoadBalancerFlags = 0 // LoadBalancerFlagsDSR enables Direct Server Return (DSR) - LoadBalancerFlagsDSR LoadBalancerFlags = 1 + LoadBalancerFlagsDSR LoadBalancerFlags = 1 LoadBalancerFlagsIPv6 LoadBalancerFlags = 2 ) @@ -161,6 +161,49 @@ func createLoadBalancer(settings string) (*HostComputeLoadBalancer, error) { return &outputLoadBalancer, nil } +func updateLoadBalancer(loadbalancerId string, settings string) (*HostComputeLoadBalancer, error) { + loadBalancerGuid, err := guid.FromString(loadbalancerId) + if err != nil { + return nil, errInvalidLoadBalancerID + } + // Update loadBalancer. + var ( + loadBalancerHandle hcnLoadBalancer + resultBuffer *uint16 + propertiesBuffer *uint16 + ) + hr := hcnOpenLoadBalancer(&loadBalancerGuid, &loadBalancerHandle, &resultBuffer) + if err := checkForErrors("hcnOpenLoadBalancer", hr, resultBuffer); err != nil { + return nil, err + } + hr = hcnModifyLoadBalancer(loadBalancerHandle, settings, &resultBuffer) + if err := checkForErrors("hcnModifyLoadBalancer", hr, resultBuffer); err != nil { + return nil, err + } + // Query loadBalancer. + hcnQuery := defaultQuery() + query, err := json.Marshal(hcnQuery) + if err != nil { + return nil, err + } + hr = hcnQueryLoadBalancerProperties(loadBalancerHandle, string(query), &propertiesBuffer, &resultBuffer) + if err := checkForErrors("hcnQueryLoadBalancerProperties", hr, resultBuffer); err != nil { + return nil, err + } + properties := interop.ConvertAndFreeCoTaskMemString(propertiesBuffer) + // Close loadBalancer. + hr = hcnCloseLoadBalancer(loadBalancerHandle) + if err := checkForErrors("hcnCloseLoadBalancer", hr, nil); err != nil { + return nil, err + } + // Convert output to HostComputeLoadBalancer + var outputLoadBalancer HostComputeLoadBalancer + if err := json.Unmarshal([]byte(properties), &outputLoadBalancer); err != nil { + return nil, err + } + return &outputLoadBalancer, nil +} + func deleteLoadBalancer(loadBalancerId string) error { loadBalancerGuid, err := guid.FromString(loadBalancerId) if err != nil { @@ -235,6 +278,23 @@ func (loadBalancer *HostComputeLoadBalancer) Create() (*HostComputeLoadBalancer, return loadBalancer, nil } +// Update Loadbalancer. +func (loadBalancer *HostComputeLoadBalancer) Update(hnsLoadbalancerID string) (*HostComputeLoadBalancer, error) { + logrus.Debugf("hcn::HostComputeLoadBalancer::Create id=%s", hnsLoadbalancerID) + + jsonString, err := json.Marshal(loadBalancer) + if err != nil { + return nil, err + } + + logrus.Debugf("hcn::HostComputeLoadBalancer::Update JSON: %s", jsonString) + loadBalancer, hcnErr := updateLoadBalancer(hnsLoadbalancerID, string(jsonString)) + if hcnErr != nil { + return nil, hcnErr + } + return loadBalancer, nil +} + // Delete LoadBalancer. func (loadBalancer *HostComputeLoadBalancer) Delete() error { logrus.Debugf("hcn::HostComputeLoadBalancer::Delete id=%s", loadBalancer.Id) diff --git a/hcn/hcnloadbalancer_test.go b/hcn/hcnloadbalancer_test.go index c4c181c9be..9924c14b77 100644 --- a/hcn/hcnloadbalancer_test.go +++ b/hcn/hcnloadbalancer_test.go @@ -41,6 +41,70 @@ func TestCreateDeleteLoadBalancer(t *testing.T) { } } +func TestCreateUpdateDeleteLoadBalancer(t *testing.T) { + network, err := CreateTestOverlayNetwork() + if err != nil { + t.Fatal(err) + } + endpoint, err := HcnCreateTestEndpoint(network) + if err != nil { + t.Fatal(err) + } + loadBalancer, err := HcnCreateTestLoadBalancer(endpoint) + if err != nil { + t.Fatal(err) + } + jsonString, err := json.Marshal(loadBalancer) + if err != nil { + t.Fatal(err) + } + fmt.Printf("LoadBalancer JSON:\n%s \n", jsonString) + + secondEndpoint, err := HcnCreateTestEndpoint(network) + if err != nil { + t.Fatal(err) + } + + HcnLoadBalancerTestAddBackend(loadBalancer, secondEndpoint.Id) + + loadBalancer, err = loadBalancer.Update(loadBalancer.Id) + if err != nil { + t.Fatal(err) + } + + if len(loadBalancer.HostComputeEndpoints) != 2 { + t.Fatalf("Update loadBalancer with backend add failed") + } + + HcnLoadBalancerTestRemoveBackend(loadBalancer, secondEndpoint.Id) + + loadBalancer, err = loadBalancer.Update(loadBalancer.Id) + if err != nil { + t.Fatal(err) + } + + if len(loadBalancer.HostComputeEndpoints) != 1 { + t.Fatalf("Update loadBalancer with backend remove failed") + } + + err = loadBalancer.Delete() + if err != nil { + t.Fatal(err) + } + err = secondEndpoint.Delete() + if err != nil { + t.Fatal(err) + } + err = endpoint.Delete() + if err != nil { + t.Fatal(err) + } + err = network.Delete() + if err != nil { + t.Fatal(err) + } +} + func TestGetLoadBalancerById(t *testing.T) { network, err := CreateTestOverlayNetwork() if err != nil { diff --git a/hcn/hcnpolicy.go b/hcn/hcnpolicy.go index 29651bb5f1..18c93ed8ca 100644 --- a/hcn/hcnpolicy.go +++ b/hcn/hcnpolicy.go @@ -141,10 +141,11 @@ type QosPolicySetting struct { // OutboundNatPolicySetting sets outbound Network Address Translation on an Endpoint. type OutboundNatPolicySetting struct { - VirtualIP string `json:",omitempty"` - Exceptions []string `json:",omitempty"` - Destinations []string `json:",omitempty"` - Flags NatFlags `json:",omitempty"` + VirtualIP string `json:",omitempty"` + Exceptions []string `json:",omitempty"` + Destinations []string `json:",omitempty"` + Flags NatFlags `json:",omitempty"` + MaxPortPoolUsage uint16 `json:",omitempty"` } // SDNRoutePolicySetting sets SDN Route on an Endpoint. diff --git a/hcn/hcnsupport.go b/hcn/hcnsupport.go index 64f9e3728b..c34d89dbec 100644 --- a/hcn/hcnsupport.go +++ b/hcn/hcnsupport.go @@ -32,6 +32,10 @@ type SupportedFeatures struct { L4Proxy bool `json:"L4Proxy"` // network policy that applies VFP rules to all endpoints on the network to redirect traffic L4WfpProxy bool `json:"L4WfpProxy"` // endpoint policy that applies WFP filters to redirect traffic to/from that endpoint TierAcl bool `json:"TierAcl"` + NetworkACL bool `json:"NetworkACL"` + NestedIpSet bool `json:"NestedIpSet"` + DisableHostPort bool `json:"DisableHostPort"` + ModifyLoadbalancer bool `json:"ModifyLoadbalancer"` } // AclFeatures are the supported ACL possibilities. @@ -107,6 +111,10 @@ func getSupportedFeatures() (SupportedFeatures, error) { features.L4Proxy = isFeatureSupported(globals.Version, L4ProxyPolicyVersion) features.L4WfpProxy = isFeatureSupported(globals.Version, L4WfpProxyPolicyVersion) features.TierAcl = isFeatureSupported(globals.Version, TierAclPolicyVersion) + features.NetworkACL = isFeatureSupported(globals.Version, NetworkACLPolicyVersion) + features.NestedIpSet = isFeatureSupported(globals.Version, NestedIpSetVersion) + features.DisableHostPort = isFeatureSupported(globals.Version, DisableHostPortVersion) + features.ModifyLoadbalancer = isFeatureSupported(globals.Version, ModifyLoadbalancerVersion) logrus.WithFields(logrus.Fields{ "version": fmt.Sprintf("%+v", globals.Version), diff --git a/hcn/hcnutils_test.go b/hcn/hcnutils_test.go index 0ecd64af34..f51bdf851b 100644 --- a/hcn/hcnutils_test.go +++ b/hcn/hcnutils_test.go @@ -290,6 +290,23 @@ func HcnCreateTestLoadBalancer(endpoint *HostComputeEndpoint) (*HostComputeLoadB return loadBalancer.Create() } +func HcnLoadBalancerTestAddBackend(loadBalancer *HostComputeLoadBalancer, endpointId string) { + endpointIds := loadBalancer.HostComputeEndpoints + endpointIds = append(endpointIds, endpointId) + loadBalancer.HostComputeEndpoints = endpointIds +} + +func HcnLoadBalancerTestRemoveBackend(loadBalancer *HostComputeLoadBalancer, endpointId string) { + endpointIds := loadBalancer.HostComputeEndpoints + for i, v := range endpointIds { + if v == endpointId { + endpointIds = append(endpointIds[:i], endpointIds[i+1:]...) + break + } + } + loadBalancer.HostComputeEndpoints = endpointIds +} + func HcnCreateTestRemoteSubnetRoute() (*PolicyNetworkRequest, error) { rsr := RemoteSubnetRoutePolicySetting{ DestinationPrefix: "192.168.2.0/24", diff --git a/internal/hns/hnspolicy.go b/internal/hns/hnspolicy.go index 6765aaead5..a8584f71ec 100644 --- a/internal/hns/hnspolicy.go +++ b/internal/hns/hnspolicy.go @@ -56,9 +56,10 @@ type PaPolicy struct { type OutboundNatPolicy struct { Policy - VIP string `json:"VIP,omitempty"` - Exceptions []string `json:"ExceptionList,omitempty"` - Destinations []string `json:",omitempty"` + VIP string `json:"VIP,omitempty"` + Exceptions []string `json:"ExceptionList,omitempty"` + Destinations []string `json:",omitempty"` + MaxPortPoolUsage uint16 `json:",omitempty"` } type ProxyPolicy struct {