Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
78a088dc
Commit
78a088dc
authored
Jul 31, 2017
by
p0lyn0mial
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adds an admission plugin to the sample apiserver.
the admission plugin checks whether Flunder.Name is not on the banned list. including a unit test with various test scenarios.
parent
a1c0510d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
281 additions
and
0 deletions
+281
-0
BUILD
...io/sample-apiserver/pkg/admission/plugin/banflunder/BUILD
+42
-0
admission.go
...le-apiserver/pkg/admission/plugin/banflunder/admission.go
+84
-0
admission_test.go
...iserver/pkg/admission/plugin/banflunder/admission_test.go
+155
-0
No files found.
staging/src/k8s.io/sample-apiserver/pkg/admission/plugin/banflunder/BUILD
0 → 100644
View file @
78a088dc
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["admission.go"],
tags = ["automanaged"],
deps = [
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/admission/wardleinitializer:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/apis/wardle:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/client/informers_generated/internalversion:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/client/listers_generated/wardle/internalversion:go_default_library",
],
)
go_test(
name = "go_default_xtest",
srcs = ["admission_test.go"],
tags = ["automanaged"],
deps = [
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
"//vendor/k8s.io/client-go/testing:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/admission/plugin/banflunder:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/admission/wardleinitializer:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/apis/wardle:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/client/clientset_generated/internalclientset/fake:go_default_library",
"//vendor/k8s.io/sample-apiserver/pkg/client/informers_generated/internalversion:go_default_library",
],
)
staging/src/k8s.io/sample-apiserver/pkg/admission/plugin/banflunder/admission.go
0 → 100644
View file @
78a088dc
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
banflunder
import
(
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apiserver/pkg/admission"
"k8s.io/sample-apiserver/pkg/admission/wardleinitializer"
"k8s.io/sample-apiserver/pkg/apis/wardle"
informers
"k8s.io/sample-apiserver/pkg/client/informers_generated/internalversion"
listers
"k8s.io/sample-apiserver/pkg/client/listers_generated/wardle/internalversion"
)
type
disallowFlunder
struct
{
*
admission
.
Handler
lister
listers
.
FischerLister
}
var
_
=
wardleinitializer
.
WantsInternalWardleInformerFactory
(
&
disallowFlunder
{})
// Admit ensures that the object in-flight is of kind Flunder.
// In addition checks that the Name is not on the banned list.
// The list is stored in Fischers API objects.
func
(
d
*
disallowFlunder
)
Admit
(
a
admission
.
Attributes
)
error
{
// we are only interested in flunders
if
a
.
GetKind
()
.
GroupKind
()
!=
wardle
.
Kind
(
"Flunder"
)
{
return
nil
}
flunderName
:=
a
.
GetName
()
fischers
,
err
:=
d
.
lister
.
List
(
labels
.
Everything
())
if
err
!=
nil
{
return
err
}
for
_
,
fischer
:=
range
fischers
{
for
_
,
disallowedFlunder
:=
range
fischer
.
DisallowedFlunders
{
if
flunderName
==
disallowedFlunder
{
return
errors
.
NewForbidden
(
a
.
GetResource
()
.
GroupResource
(),
a
.
GetName
(),
fmt
.
Errorf
(
"this name may not be used, please change the resource name"
),
)
}
}
}
return
nil
}
// SetInternalWardleInformerFactory gets Lister from SharedInformerFactory.
// The lister knows how to lists Fischers.
func
(
d
*
disallowFlunder
)
SetInternalWardleInformerFactory
(
f
informers
.
SharedInformerFactory
)
{
d
.
lister
=
f
.
Wardle
()
.
InternalVersion
()
.
Fischers
()
.
Lister
()
}
// Validate checks whether the plugin was correctly initialized.
func
(
d
*
disallowFlunder
)
Validate
()
error
{
if
d
.
lister
==
nil
{
return
fmt
.
Errorf
(
"missing fischer lister"
)
}
return
nil
}
// New creates a new ban flunder admission plugin
func
New
()
(
admission
.
Interface
,
error
)
{
return
&
disallowFlunder
{},
nil
}
staging/src/k8s.io/sample-apiserver/pkg/admission/plugin/banflunder/admission_test.go
0 → 100644
View file @
78a088dc
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
banflunder_test
import
(
"testing"
"time"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/admission"
clienttesting
"k8s.io/client-go/testing"
"k8s.io/sample-apiserver/pkg/admission/plugin/banflunder"
"k8s.io/sample-apiserver/pkg/admission/wardleinitializer"
"k8s.io/sample-apiserver/pkg/apis/wardle"
"k8s.io/sample-apiserver/pkg/client/clientset_generated/internalclientset/fake"
informers
"k8s.io/sample-apiserver/pkg/client/informers_generated/internalversion"
)
// TestBanfluderAdmissionPlugin tests various test cases against
// ban flunder admission plugin
func
TestBanflunderAdmissionPlugin
(
t
*
testing
.
T
)
{
var
scenarios
=
[]
struct
{
informersOutput
wardle
.
FischerList
admissionInput
wardle
.
Flunder
admissionInputKind
schema
.
GroupVersionKind
admissionInputResource
schema
.
GroupVersionResource
admissionMustFail
bool
}{
// scenario 1:
// a flunder with a name that appears on a list of disallowed flunders must be banned
{
informersOutput
:
wardle
.
FischerList
{
Items
:
[]
wardle
.
Fischer
{
{
DisallowedFlunders
:
[]
string
{
"badname"
}},
},
},
admissionInput
:
wardle
.
Flunder
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"badname"
,
Namespace
:
""
,
},
},
admissionInputKind
:
wardle
.
Kind
(
"Flunder"
)
.
WithVersion
(
"version"
),
admissionInputResource
:
wardle
.
Resource
(
"flunders"
)
.
WithVersion
(
"version"
),
admissionMustFail
:
true
,
},
// scenario 2:
// a flunder with a name that does not appear on a list of disallowed flunders must be admitted
{
informersOutput
:
wardle
.
FischerList
{
Items
:
[]
wardle
.
Fischer
{
{
DisallowedFlunders
:
[]
string
{
"badname"
}},
},
},
admissionInput
:
wardle
.
Flunder
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"goodname"
,
Namespace
:
""
,
},
},
admissionInputKind
:
wardle
.
Kind
(
"Flunder"
)
.
WithVersion
(
"version"
),
admissionInputResource
:
wardle
.
Resource
(
"flunders"
)
.
WithVersion
(
"version"
),
admissionMustFail
:
false
,
},
// scenario 3:
// a flunder with a name that appears on a list of disallowed flunders would be banned
// but the kind passed in is not a flunder thus the whole request is accepted
{
informersOutput
:
wardle
.
FischerList
{
Items
:
[]
wardle
.
Fischer
{
{
DisallowedFlunders
:
[]
string
{
"badname"
}},
},
},
admissionInput
:
wardle
.
Flunder
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"badname"
,
Namespace
:
""
,
},
},
admissionInputKind
:
wardle
.
Kind
(
"NotFlunder"
)
.
WithVersion
(
"version"
),
admissionInputResource
:
wardle
.
Resource
(
"notflunders"
)
.
WithVersion
(
"version"
),
admissionMustFail
:
false
,
},
}
for
index
,
scenario
:=
range
scenarios
{
func
()
{
// prepare
cs
:=
&
fake
.
Clientset
{}
cs
.
AddReactor
(
"list"
,
"fischers"
,
func
(
action
clienttesting
.
Action
)
(
bool
,
runtime
.
Object
,
error
)
{
return
true
,
&
scenario
.
informersOutput
,
nil
})
informersFactory
:=
informers
.
NewSharedInformerFactory
(
cs
,
5
*
time
.
Minute
)
target
,
err
:=
banflunder
.
New
()
if
err
!=
nil
{
t
.
Fatalf
(
"scenario %d: failed to create banflunder admission plugin due to = %v"
,
index
,
err
)
}
targetInitializer
,
err
:=
wardleinitializer
.
New
(
informersFactory
)
if
err
!=
nil
{
t
.
Fatalf
(
"scenario %d: failed to crate wardle plugin initializer due to = %v"
,
index
,
err
)
}
targetInitializer
.
Initialize
(
target
)
err
=
admission
.
Validate
(
target
)
if
err
!=
nil
{
t
.
Fatalf
(
"scenario %d: failed to initialize banflunder admission plugin due to =%v"
,
index
,
err
)
}
stop
:=
make
(
chan
struct
{})
defer
close
(
stop
)
informersFactory
.
Start
(
stop
)
informersFactory
.
WaitForCacheSync
(
stop
)
// act
err
=
target
.
Admit
(
admission
.
NewAttributesRecord
(
&
scenario
.
admissionInput
,
nil
,
scenario
.
admissionInputKind
,
scenario
.
admissionInput
.
ObjectMeta
.
Namespace
,
scenario
.
admissionInput
.
ObjectMeta
.
Name
,
scenario
.
admissionInputResource
,
""
,
admission
.
Create
,
nil
),
)
// validate
if
scenario
.
admissionMustFail
&&
err
==
nil
{
t
.
Errorf
(
"scenario %d: expected an error but got nothing"
,
index
)
}
if
!
scenario
.
admissionMustFail
&&
err
!=
nil
{
t
.
Errorf
(
"scenario %d: banflunder admission plugin returned unexpected error = %v"
,
index
,
err
)
}
}()
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment