k8s.io/apiserver@v0.31.1/pkg/audit/policy/util.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes 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 policy
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/util/sets"
    21  	"k8s.io/apiserver/pkg/apis/audit"
    22  )
    23  
    24  // AllStages returns all possible stages
    25  func AllStages() sets.String {
    26  	return sets.NewString(
    27  		string(audit.StageRequestReceived),
    28  		string(audit.StageResponseStarted),
    29  		string(audit.StageResponseComplete),
    30  		string(audit.StagePanic),
    31  	)
    32  }
    33  
    34  // AllLevels returns all possible levels
    35  func AllLevels() sets.String {
    36  	return sets.NewString(
    37  		string(audit.LevelNone),
    38  		string(audit.LevelMetadata),
    39  		string(audit.LevelRequest),
    40  		string(audit.LevelRequestResponse),
    41  	)
    42  }
    43  
    44  // InvertStages subtracts the given array of stages from all stages
    45  func InvertStages(stages []audit.Stage) []audit.Stage {
    46  	s := ConvertStagesToStrings(stages)
    47  	a := AllStages()
    48  	a.Delete(s...)
    49  	return ConvertStringSetToStages(a)
    50  }
    51  
    52  // ConvertStagesToStrings converts an array of stages to a string array
    53  func ConvertStagesToStrings(stages []audit.Stage) []string {
    54  	s := make([]string, len(stages))
    55  	for i, stage := range stages {
    56  		s[i] = string(stage)
    57  	}
    58  	return s
    59  }
    60  
    61  // ConvertStringSetToStages converts a string set to an array of stages
    62  func ConvertStringSetToStages(set sets.String) []audit.Stage {
    63  	stages := make([]audit.Stage, len(set))
    64  	for i, stage := range set.List() {
    65  		stages[i] = audit.Stage(stage)
    66  	}
    67  	return stages
    68  }