github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/pkg/apis/componentconfig/edgecore/v1alpha1/validation/validation.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package validation
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  
    26  	"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/edgecore/v1alpha1"
    27  	utilvalidation "github.com/kubeedge/kubeedge/pkg/util/validation"
    28  )
    29  
    30  // ValidateEdgeCoreConfiguration validates `c` and returns an errorList if it is invalid
    31  func ValidateEdgeCoreConfiguration(c *v1alpha1.EdgeCoreConfig) field.ErrorList {
    32  	allErrs := field.ErrorList{}
    33  	allErrs = append(allErrs, ValidateDataBase(*c.DataBase)...)
    34  	allErrs = append(allErrs, ValidateModuleEdged(*c.Modules.Edged)...)
    35  	allErrs = append(allErrs, ValidateModuleEdgeHub(*c.Modules.EdgeHub)...)
    36  	allErrs = append(allErrs, ValidateModuleEventBus(*c.Modules.EventBus)...)
    37  	allErrs = append(allErrs, ValidateModuleMetaManager(*c.Modules.MetaManager)...)
    38  	allErrs = append(allErrs, ValidateModuleServiceBus(*c.Modules.ServiceBus)...)
    39  	allErrs = append(allErrs, ValidateModuleDeviceTwin(*c.Modules.DeviceTwin)...)
    40  	allErrs = append(allErrs, ValidateModuleDBTest(*c.Modules.DBTest)...)
    41  	allErrs = append(allErrs, ValidateModuleEdgeMesh(*c.Modules.EdgeMesh)...)
    42  	return allErrs
    43  }
    44  
    45  // ValidateDataBase validates `db` and returns an errorList if it is invalid
    46  func ValidateDataBase(db v1alpha1.DataBase) field.ErrorList {
    47  	allErrs := field.ErrorList{}
    48  	sourceDir := path.Dir(db.DataSource)
    49  	if !utilvalidation.FileIsExist(sourceDir) {
    50  		if err := os.MkdirAll(sourceDir, os.ModePerm); err != nil {
    51  			allErrs = append(allErrs, field.Invalid(field.NewPath("DataSource"), db.DataSource,
    52  				fmt.Sprintf("create DataSoure dir %v error ", sourceDir)))
    53  		}
    54  	}
    55  	return allErrs
    56  }
    57  
    58  // ValidateModuleEdged validates `e` and returns an errorList if it is invalid
    59  func ValidateModuleEdged(e v1alpha1.Edged) field.ErrorList {
    60  	if !e.Enable {
    61  		return field.ErrorList{}
    62  	}
    63  	allErrs := field.ErrorList{}
    64  	if e.NodeIP == "" {
    65  		allErrs = append(allErrs, field.Invalid(field.NewPath("NodeIp"), e.NodeIP,
    66  			"Need sed NodeIP"))
    67  	}
    68  	switch e.CGroupDriver {
    69  	case v1alpha1.CGroupDriverCGroupFS, v1alpha1.CGroupDriverSystemd:
    70  	default:
    71  		allErrs = append(allErrs, field.Invalid(field.NewPath("CGroupDriver"), e.CGroupDriver,
    72  			"CGroupDriver value error"))
    73  	}
    74  	return allErrs
    75  }
    76  
    77  // ValidateModuleEdgeHub validates `h` and returns an errorList if it is invalid
    78  func ValidateModuleEdgeHub(h v1alpha1.EdgeHub) field.ErrorList {
    79  	if !h.Enable {
    80  		return field.ErrorList{}
    81  	}
    82  	allErrs := field.ErrorList{}
    83  	if !utilvalidation.FileIsExist(h.TLSPrivateKeyFile) {
    84  		allErrs = append(allErrs, field.Invalid(field.NewPath("TLSPrivateKeyFile"),
    85  			h.TLSPrivateKeyFile, "TLSPrivateKeyFile not exist"))
    86  	}
    87  	if !utilvalidation.FileIsExist(h.TLSCertFile) {
    88  		allErrs = append(allErrs, field.Invalid(field.NewPath("TLSCertFile"),
    89  			h.TLSCertFile, "TLSCertFile not exist"))
    90  	}
    91  
    92  	// Comments out the steps to verify CA certificate
    93  	/*
    94  		if !utilvalidation.FileIsExist(h.TLSCAFile) {
    95  			allErrs = append(allErrs, field.Invalid(field.NewPath("TLSCAFile"),
    96  				h.TLSCAFile, "TLSCAFile not exist"))
    97  		}
    98  	*/
    99  	if h.WebSocket.Enable == h.Quic.Enable {
   100  		allErrs = append(allErrs, field.Invalid(field.NewPath("enable"),
   101  			h.Quic.Enable, "websocket.enable and quic.enable cannot be true and false at the same time"))
   102  	}
   103  
   104  	return allErrs
   105  }
   106  
   107  // ValidateModuleEventBus validates `m` and returns an errorList if it is invalid
   108  func ValidateModuleEventBus(m v1alpha1.EventBus) field.ErrorList {
   109  	if !m.Enable {
   110  		return field.ErrorList{}
   111  	}
   112  	allErrs := field.ErrorList{}
   113  	if m.MqttMode > v1alpha1.MqttModeExternal || m.MqttMode < v1alpha1.MqttModeInternal {
   114  		allErrs = append(allErrs, field.Invalid(field.NewPath("Mode"), m.MqttMode,
   115  			fmt.Sprintf("Mode need in [%v,%v] range", v1alpha1.MqttModeInternal,
   116  				v1alpha1.MqttModeExternal)))
   117  	}
   118  	return allErrs
   119  }
   120  
   121  // ValidateModuleMetaManager validates `m` and returns an errorList if it is invalid
   122  func ValidateModuleMetaManager(m v1alpha1.MetaManager) field.ErrorList {
   123  	if !m.Enable {
   124  		return field.ErrorList{}
   125  	}
   126  	allErrs := field.ErrorList{}
   127  	return allErrs
   128  }
   129  
   130  // ValidateModuleServiceBus validates `s` and returns an errorList if it is invalid
   131  func ValidateModuleServiceBus(s v1alpha1.ServiceBus) field.ErrorList {
   132  	if !s.Enable {
   133  		return field.ErrorList{}
   134  	}
   135  	allErrs := field.ErrorList{}
   136  	return allErrs
   137  }
   138  
   139  // ValidateModuleDeviceTwin validates `d` and returns an errorList if it is invalid
   140  func ValidateModuleDeviceTwin(d v1alpha1.DeviceTwin) field.ErrorList {
   141  	if !d.Enable {
   142  		return field.ErrorList{}
   143  	}
   144  	allErrs := field.ErrorList{}
   145  	return allErrs
   146  }
   147  
   148  // ValidateModuleDBTest validates `d` and returns an errorList if it is invalid
   149  func ValidateModuleDBTest(d v1alpha1.DBTest) field.ErrorList {
   150  	if !d.Enable {
   151  		return field.ErrorList{}
   152  	}
   153  	allErrs := field.ErrorList{}
   154  	return allErrs
   155  }
   156  
   157  // ValidateModuleEdgeMesh validates `m` and returns an errorList if it is invalid
   158  func ValidateModuleEdgeMesh(m v1alpha1.EdgeMesh) field.ErrorList {
   159  	if !m.Enable {
   160  		return field.ErrorList{}
   161  	}
   162  	// TODO check meshconfig @kadisi
   163  	allErrs := field.ErrorList{}
   164  	return allErrs
   165  }