Skip to content

Commit

Permalink
validate instanceView state and provingingState
Browse files Browse the repository at this point in the history
Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>
  • Loading branch information
helayoty committed Jan 24, 2023
1 parent ef04817 commit fb72a69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
10 changes: 7 additions & 3 deletions pkg/provider/aci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
fakeRegion = getEnv("LOCATION", "westus2")
creationTime = "2006-01-02 15:04:05.999999999 -0700 MST"
azConfig auth.Config
runningState = "Running"
)

func getEnv(key, fallback string) string {
Expand Down Expand Up @@ -401,7 +402,10 @@ func TestGetPodsWithoutResourceRequestsLimits(t *testing.T) {
},
Properties: &azaciv2.ContainerGroupPropertiesProperties{
ProvisioningState: &provisioning,
Containers: testsutil.CreateACIContainersListObj("Running", "Initializing", testsutil.CgCreationTime.Add(time.Second*2), testsutil.CgCreationTime.Add(time.Second*3), true, false, false),
Containers: testsutil.CreateACIContainersListObj(runningState, "Initializing", testsutil.CgCreationTime.Add(time.Second*2), testsutil.CgCreationTime.Add(time.Second*3), true, false, false),
InstanceView: &azaciv2.ContainerGroupPropertiesInstanceView{
State: &runningState,
},
},
}
var result []*azaciv2.ContainerGroup
Expand Down Expand Up @@ -442,15 +446,15 @@ func TestGetPodWithoutResourceRequestsLimits(t *testing.T) {
aciMocks.MockGetContainerGroupInfo =
func(ctx context.Context, resourceGroup, namespace, name, nodeName string) (*azaciv2.ContainerGroup, error) {
return testsutil.CreateContainerGroupObj(podName, podNamespace, "Succeeded",
testsutil.CreateACIContainersListObj("Running", "Initializing",
testsutil.CreateACIContainersListObj(runningState, "Initializing",
testsutil.CgCreationTime.Add(time.Second*2),
testsutil.CgCreationTime.Add(time.Second*3),
false, false, false), "Succeeded"), nil
}

aciMocks.MockGetContainerGroupList = func(ctx context.Context, resourceGroup string) ([]*azaciv2.ContainerGroup, error) {
cg := testsutil.CreateContainerGroupObj(podName, podNamespace, "Succeeded",
testsutil.CreateACIContainersListObj("Running", "Initializing",
testsutil.CreateACIContainersListObj(runningState, "Initializing",
testsutil.CgCreationTime.Add(time.Second*2),
testsutil.CgCreationTime.Add(time.Second*3),
false, false, false), "Succeeded")
Expand Down
17 changes: 10 additions & 7 deletions pkg/provider/containergroup_to_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (p *ACIProvider) getPodStatusFromContainerGroup(ctx context.Context, cg *az

// cg is validated
allReady := true
var firstContainerStartTime, lastUpdateTime *time.Time
var firstContainerStartTime, lastUpdateTime time.Time

containerStatuses := make([]v1.ContainerStatus, 0, len(cg.Properties.Containers))
containersList := cg.Properties.Containers
Expand All @@ -54,8 +54,11 @@ func (p *ACIProvider) getPodStatusFromContainerGroup(ctx context.Context, cg *az
return nil, err
}

firstContainerStartTime = containersList[i].Properties.InstanceView.CurrentState.StartTime
lastUpdateTime = firstContainerStartTime
// init the firstContainerStartTime & lastUpdateTime
if i == 0 {
firstContainerStartTime = *containersList[0].Properties.InstanceView.CurrentState.StartTime
lastUpdateTime = firstContainerStartTime
}

containerStatus := v1.ContainerStatus{
Name: *containersList[i].Name,
Expand All @@ -74,8 +77,8 @@ func (p *ACIProvider) getPodStatusFromContainerGroup(ctx context.Context, cg *az
}

containerStartTime := containersList[i].Properties.InstanceView.CurrentState.StartTime
if containerStartTime.After(*lastUpdateTime) {
lastUpdateTime = containerStartTime
if containerStartTime.After(lastUpdateTime) {
lastUpdateTime = *containerStartTime
}

// Add to containerStatuses
Expand All @@ -94,12 +97,12 @@ func (p *ACIProvider) getPodStatusFromContainerGroup(ctx context.Context, cg *az
}
return &v1.PodStatus{
Phase: getPodPhaseFromACIState(*aciState),
Conditions: getPodConditionsFromACIState(*aciState, creationTime, *lastUpdateTime, allReady),
Conditions: getPodConditionsFromACIState(*aciState, creationTime, lastUpdateTime, allReady),
Message: "",
Reason: "",
HostIP: p.internalIP,
PodIP: podIp,
StartTime: &metav1.Time{Time: *firstContainerStartTime},
StartTime: &metav1.Time{Time: firstContainerStartTime},
ContainerStatuses: containerStatuses,
}, nil
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,20 @@ func ValidateContainerGroup(ctx context.Context, cg *azaciv2.ContainerGroup) err
if cg.Tags == nil {
return errors.Errorf("tags list cannot be nil for container group %s", *cg.Name)
}
if cg.Properties.InstanceView == nil {
return errors.Errorf("InstanceView cannot be nil for container group %s", *cg.Name)
}
if cg.Properties.InstanceView.State == nil {
return errors.Errorf("InstanceView state cannot be nil for container group %s", *cg.Name)
}
if cg.Properties.OSType != nil &&
*cg.Properties.OSType != azaciv2.OperatingSystemTypesWindows {
if cg.Properties.IPAddress == nil {
return errors.Errorf("IPAddress cannot be nil for container group %s", *cg.Name)
} else {
if cg.Properties.ProvisioningState == nil {
return errors.Errorf("ProvisioningState cannot be nil for container group %s", *cg.Name)
}
aciState := *cg.Properties.ProvisioningState
if cg.Properties.IPAddress.IP == nil {
if aciState == "Running" {
Expand Down

0 comments on commit fb72a69

Please sign in to comment.