Skip to content

Commit

Permalink
Merge pull request #49 from przemeklal/fix_null_pointer_and_error_han…
Browse files Browse the repository at this point in the history
…dling

Improve error handling
  • Loading branch information
garyloug authored Oct 14, 2020
2 parents b11a037 + 14d5bc9 commit 0b9661a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
8 changes: 1 addition & 7 deletions cniovs/cniovs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018 Red Hat.
// Copyright (c) 2018-2020 Red Hat, Intel Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -123,17 +123,11 @@ func (cniOvs CniOvs) AddOnHost(conf *types.NetConf,
logging.Debugf("AddOnHost(ovs): %v", err)
return err
}
if err != nil {
return err
}

//
// Save Config - Save Create Data for Delete
//
err = SaveConfig(conf, args, &data)
if err != nil {
return err
}

return err
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,19 @@ type NoKubeClientProvidedError struct {

func (e *NoKubeClientProvidedError) Error() string { return string(e.message) }

type NoPodProvidedError struct {
message string
}

func (e *NoPodProvidedError) Error() string { return string(e.message) }

func GetPodVolumeMountHostSharedDir(pod *v1.Pod) (string, error) {
var hostSharedDir string

if pod == nil {
return hostSharedDir, &NoPodProvidedError{"Error: Pod not provided."}
}

logging.Verbosef("GetPodVolumeMountSharedDir: type=%T Volumes=%v", pod.Spec.Volumes, pod.Spec.Volumes)

if len(pod.Spec.Volumes) == 0 {
Expand Down Expand Up @@ -91,6 +101,10 @@ func GetPodVolumeMountHostSharedDir(pod *v1.Pod) (string, error) {
func getPodVolumeMountHostMappedSharedDir(pod *v1.Pod) (string, error) {
var mappedSharedDir string

if pod == nil {
return mappedSharedDir, &NoPodProvidedError{"Error: Pod not provided."}
}

logging.Verbosef("getPodVolumeMountHostMappedSharedDir: Containers=%v", pod.Spec.Containers)

if len(pod.Spec.Containers) == 0 {
Expand Down Expand Up @@ -122,6 +136,10 @@ func WritePodAnnotation(kubeClient kubernetes.Interface,
var modifiedConfig bool
var modifiedMappedDir bool

if pod == nil {
return pod, &NoPodProvidedError{"Error: Pod not provided."}
}

//
// Write configuration data that will be consumed by container
//
Expand Down Expand Up @@ -174,6 +192,10 @@ func setPodAnnotationMappedDir(pod *v1.Pod,
mappedDir string) (bool, error) {
var modified bool

if pod == nil {
return false, &NoPodProvidedError{"Error: Pod not provided."}
}

logging.Verbosef("SetPodAnnotationMappedDir: inputMappedDir=%s Annot - type=%T mappedDir=%v", mappedDir, pod.Annotations[AnnotKeyUsrspMappedDir], pod.Annotations[AnnotKeyUsrspMappedDir])

// If pod annotations is empty, make sure it allocatable
Expand Down Expand Up @@ -207,6 +229,16 @@ func setPodAnnotationConfigData(pod *v1.Pod,
var configDataStr []string
var modified bool

if pod == nil {
return false, &NoPodProvidedError{"Error: Pod not provided."}
}

// check for empty configData, otherwise "null" string would be added to annotations
if configData == nil {
logging.Verbosef("SetPodAnnotationConfigData: ConfigData not provided: %v", configData)
return false, nil
}

logging.Verbosef("SetPodAnnotationConfigData: type=%T configData=%v", pod.Annotations[AnnotKeyUsrspConfigData], pod.Annotations[AnnotKeyUsrspConfigData])

// If pod annotations is empty, make sure it allocatable
Expand Down
12 changes: 12 additions & 0 deletions pkg/configdata/configdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func SaveRemoteConfig(conf *types.NetConf,
var configData types.ConfigurationData
var err error

// Check if required parameters are set and fail otherwise
if conf == nil {
return pod, logging.Errorf("SaveRemoteConfig(): Error conf is set to: %v", conf)
}
if args == nil {
return pod, logging.Errorf("SaveRemoteConfig(): Error args is set to: %v", args)
}
if pod == nil {
return pod, logging.Errorf("SaveRemoteConfig(): Error pod is set to: %v", pod)
}
//
// Convert Local Data to types.ConfigurationData, which
// will be written to the container.
Expand Down Expand Up @@ -162,6 +172,8 @@ func SaveRemoteConfig(conf *types.NetConf,
// CleanupRemoteConfig() - This function cleans up any remaining files
// in the passed in directory. Some of these files were used to squirrel
// data from the create so interface can be deleted properly.
//
// FIXME: parameter *conf* is not used. It shall be used or removed.
func CleanupRemoteConfig(conf *types.NetConf, sharedDir string) {

if err := os.RemoveAll(sharedDir); err != nil {
Expand Down
16 changes: 10 additions & 6 deletions pkg/k8sclient/k8sclient.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Intel Corp.
// Copyright 2017-2020 Intel Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,11 +40,14 @@ type k8sArgs struct {
K8S_POD_INFRA_CONTAINER_ID cnitypes.UnmarshallableString
}


func getK8sArgs(args *skel.CmdArgs) (*k8sArgs, error) {
k8sArgs := &k8sArgs{}

logging.Verbosef("getK8sArgs: %v", args)

if args == nil {
return nil, logging.Errorf("getK8sArgs: failed to get k8s args for CmdArgs set to %v", args)
}
err := cnitypes.LoadArgs(args.Args, k8sArgs)
if err != nil {
return nil, err
Expand Down Expand Up @@ -113,8 +116,7 @@ func GetPod(args *skel.CmdArgs,
}

if kubeClient == nil {
logging.Errorf("GetPod: No kubeClient: %v", err)
return nil, kubeClient, err
return nil, nil, logging.Errorf("GetPod: No kubeClient: %v", err)
}

// Get the pod info. If cannot get it, we use cached delegates
Expand All @@ -135,8 +137,10 @@ func WritePodAnnotation(kubeClient kubernetes.Interface, pod *v1.Pod) (*v1.Pod,
var err error

if kubeClient == nil {
logging.Errorf("WritePodAnnotation: No kubeClient: %v", err)
return pod, err
return pod, logging.Errorf("WritePodAnnotation: No kubeClient: %v", err)
}
if pod == nil {
return pod, logging.Errorf("WritePodAnnotation: No pod: %v", err)
}

// Update the pod
Expand Down

0 comments on commit 0b9661a

Please sign in to comment.