diff --git a/cniovs/cniovs.go b/cniovs/cniovs.go index bac5d213..fd890978 100644 --- a/cniovs/cniovs.go +++ b/cniovs/cniovs.go @@ -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. @@ -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 } diff --git a/pkg/annotations/annotations.go b/pkg/annotations/annotations.go index d5ef327e..2130c30e 100644 --- a/pkg/annotations/annotations.go +++ b/pkg/annotations/annotations.go @@ -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 { @@ -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 { @@ -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 // @@ -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 @@ -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 diff --git a/pkg/configdata/configdata.go b/pkg/configdata/configdata.go index 9ec2d12f..848a9d65 100644 --- a/pkg/configdata/configdata.go +++ b/pkg/configdata/configdata.go @@ -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. @@ -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 { diff --git a/pkg/k8sclient/k8sclient.go b/pkg/k8sclient/k8sclient.go index b3d67ef4..67f84135 100644 --- a/pkg/k8sclient/k8sclient.go +++ b/pkg/k8sclient/k8sclient.go @@ -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. @@ -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 @@ -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 @@ -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