Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: microsoft/hcsshim
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.11.5
Choose a base ref
...
head repository: microsoft/hcsshim
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.11.6
Choose a head ref
  • 4 commits
  • 10 files changed
  • 4 contributors

Commits on Jun 19, 2024

  1. OutBoundNATPolicy Schema changes (#2106)

    Signed-off-by: Debjit Mondal <debjitmondal@microsoft.com>
    Signed-off-by: Prince Pereira <ppereira@microsoft.com>
    mdebjit authored and kiashok committed Jun 19, 2024
    Copy the full SHA
    cd46569 View commit details
  2. Changes for checking the global version for modify policy version sup…

    …port. (#2139)
    
    Signed-off-by: Prince Pereira <ppereira@microsoft.com>
    princepereira authored and kiashok committed Jun 19, 2024
    Copy the full SHA
    eefee26 View commit details
  3. Adding support for loadbalancer policy update in hns. (#2085)

    Signed-off-by: Prince Pereira <ppereira@microsoft.com>
    princepereira authored and kiashok committed Jun 19, 2024
    Copy the full SHA
    1495e9f View commit details

Commits on Jun 24, 2024

  1. Adding state attribute to the HNSEndpoint struct to support hyperv co…

    …ntainers for k8s
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Adding stringer for usage and CI/CD
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Fixing build errors
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Ignore linting for files generated by Stringer
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Trying to fix CI go gen
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Removing extra step to fix CI go gen
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    go gen CI fix try 2
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Skip autogenerated file from linting
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Fixing linting
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Fixing linting
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Removing stringer to avoid increasing package bloat for hcsshim
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    cleanup
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Adding comment for future HNS v2 change
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    
    Fix linting
    
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    (cherry picked from commit e96bfcd)
    Signed-off-by: ritikaguptams <85255050+ritikaguptams@users.noreply.github.com>
    ritikaguptams authored and kiashok committed Jun 24, 2024
    Copy the full SHA
    e59d3d2 View commit details
Showing with 199 additions and 7 deletions.
  1. +12 −0 hcn/hcn.go
  2. +5 −0 hcn/hcnerrors.go
  3. +5 −0 hcn/hcnglobals.go
  4. +60 −0 hcn/hcnloadbalancer.go
  5. +64 −0 hcn/hcnloadbalancer_test.go
  6. +5 −4 hcn/hcnpolicy.go
  7. +4 −0 hcn/hcnsupport.go
  8. +17 −0 hcn/hcnutils_test.go
  9. +23 −0 internal/hns/hnsendpoint.go
  10. +4 −3 internal/hns/hnspolicy.go
12 changes: 12 additions & 0 deletions hcn/hcn.go
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions hcn/hcnerrors.go
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ type ErrorCode uint32
const (
ERROR_NOT_FOUND = ErrorCode(windows.ERROR_NOT_FOUND)
HCN_E_PORT_ALREADY_EXISTS ErrorCode = ErrorCode(windows.HCN_E_PORT_ALREADY_EXISTS)
HCN_E_NOTIMPL ErrorCode = ErrorCode(windows.E_NOTIMPL)
)

type HcnError struct {
@@ -78,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)
5 changes: 5 additions & 0 deletions hcn/hcnglobals.go
Original file line number Diff line number Diff line change
@@ -84,6 +84,11 @@ var (

//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.
60 changes: 60 additions & 0 deletions hcn/hcnloadbalancer.go
Original file line number Diff line number Diff line change
@@ -163,6 +163,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 {
@@ -237,6 +280,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)
64 changes: 64 additions & 0 deletions hcn/hcnloadbalancer_test.go
Original file line number Diff line number Diff line change
@@ -42,6 +42,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 {
9 changes: 5 additions & 4 deletions hcn/hcnpolicy.go
Original file line number Diff line number Diff line change
@@ -144,10 +144,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.
4 changes: 4 additions & 0 deletions hcn/hcnsupport.go
Original file line number Diff line number Diff line change
@@ -37,6 +37,8 @@ type SupportedFeatures struct {
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.
@@ -114,6 +116,8 @@ func getSupportedFeatures() (SupportedFeatures, error) {
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)

log.L.WithFields(logrus.Fields{
"version": globals.Version,
17 changes: 17 additions & 0 deletions hcn/hcnutils_test.go
Original file line number Diff line number Diff line change
@@ -317,6 +317,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",
23 changes: 23 additions & 0 deletions internal/hns/hnsendpoint.go
Original file line number Diff line number Diff line change
@@ -10,6 +10,28 @@ import (
"github.com/sirupsen/logrus"
)

// EndpointState represents the states of an HNS Endpoint lifecycle.
type EndpointState uint16

// EndpointState const
// The lifecycle of an Endpoint goes through created, attached, AttachedSharing - endpoint is being shared with other containers,
// detached, after being attached, degraded and finally destroyed.
// Note: This attribute is used by calico to define stale containers and is dependent on HNS v1 api, if we move to HNS v2 api we will need
// to update the current calico code and cordinate the change with calico. Reach out to Microsoft to facilate the change via HNS.
const (
Uninitialized EndpointState = iota
Created EndpointState = 1
Attached EndpointState = 2
AttachedSharing EndpointState = 3
Detached EndpointState = 4
Degraded EndpointState = 5
Destroyed EndpointState = 6
)

func (es EndpointState) String() string {
return [...]string{"Uninitialized", "Attached", "AttachedSharing", "Detached", "Degraded", "Destroyed"}[es]
}

// HNSEndpoint represents a network endpoint in HNS
type HNSEndpoint struct {
Id string `json:"ID,omitempty"`
@@ -34,6 +56,7 @@ type HNSEndpoint struct {
Namespace *Namespace `json:",omitempty"`
EncapOverhead uint16 `json:",omitempty"`
SharedContainers []string `json:",omitempty"`
State EndpointState `json:",omitempty"`
}

// SystemType represents the type of the system on which actions are done
7 changes: 4 additions & 3 deletions internal/hns/hnspolicy.go
Original file line number Diff line number Diff line change
@@ -57,9 +57,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 {