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
ab053a22
Commit
ab053a22
authored
Nov 03, 2017
by
Chao Xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
let validation webhook convert objects to the external version before sending them
parent
8c005ddd
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
38 changed files
with
1478 additions
and
69 deletions
+1478
-69
.golint_failures
hack/.golint_failures
+2
-0
local-up-cluster.sh
hack/local-up-cluster.sh
+1
-1
update-generated-protobuf-dockerized.sh
hack/update-generated-protobuf-dockerized.sh
+1
-0
BUILD
pkg/generated/openapi/BUILD
+1
-0
BUILD
staging/BUILD
+1
-0
resources.go
...nsions-apiserver/test/integration/testserver/resources.go
+10
-2
BUILD
...g/src/k8s.io/apiserver/pkg/admission/plugin/webhook/BUILD
+6
-0
admission.go
...8s.io/apiserver/pkg/admission/plugin/webhook/admission.go
+95
-17
conversion_test.go
...apiserver/pkg/admission/plugin/webhook/conversion_test.go
+135
-0
install.go
.../src/k8s.io/apiserver/pkg/apis/example/install/install.go
+1
-1
register.go
staging/src/k8s.io/apiserver/pkg/apis/example/register.go
+1
-0
types.go
staging/src/k8s.io/apiserver/pkg/apis/example/types.go
+32
-0
zz_generated.deepcopy.go
...8s.io/apiserver/pkg/apis/example/zz_generated.deepcopy.go
+61
-0
BUILD
staging/src/k8s.io/apiserver/pkg/apis/example2/BUILD
+38
-0
doc.go
staging/src/k8s.io/apiserver/pkg/apis/example2/doc.go
+24
-0
BUILD
staging/src/k8s.io/apiserver/pkg/apis/example2/install/BUILD
+45
-0
install.go
...src/k8s.io/apiserver/pkg/apis/example2/install/install.go
+44
-0
roundtrip_test.go
....io/apiserver/pkg/apis/example2/install/roundtrip_test.go
+28
-0
register.go
staging/src/k8s.io/apiserver/pkg/apis/example2/register.go
+52
-0
BUILD
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/BUILD
+50
-0
conversion.go
...g/src/k8s.io/apiserver/pkg/apis/example2/v1/conversion.go
+51
-0
defaults.go
...ing/src/k8s.io/apiserver/pkg/apis/example2/v1/defaults.go
+26
-0
doc.go
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/doc.go
+24
-0
generated.pb.go
...src/k8s.io/apiserver/pkg/apis/example2/v1/generated.pb.go
+0
-0
generated.proto
...src/k8s.io/apiserver/pkg/apis/example2/v1/generated.proto
+71
-0
register.go
...ing/src/k8s.io/apiserver/pkg/apis/example2/v1/register.go
+63
-0
types.go
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/types.go
+64
-0
types_swagger_doc_generated.go
...erver/pkg/apis/example2/v1/types_swagger_doc_generated.go
+17
-0
zz_generated.conversion.go
...apiserver/pkg/apis/example2/v1/zz_generated.conversion.go
+111
-0
zz_generated.deepcopy.go
...o/apiserver/pkg/apis/example2/v1/zz_generated.deepcopy.go
+95
-0
zz_generated.defaults.go
...o/apiserver/pkg/apis/example2/v1/zz_generated.defaults.go
+32
-0
zz_generated.deepcopy.go
...s.io/apiserver/pkg/apis/example2/zz_generated.deepcopy.go
+21
-0
BUILD
test/e2e/apimachinery/BUILD
+1
-0
webhook.go
test/e2e/apimachinery/webhook.go
+158
-13
BUILD
test/images/webhook/BUILD
+4
-0
Makefile
test/images/webhook/Makefile
+3
-2
main.go
test/images/webhook/main.go
+73
-33
scheme.go
test/images/webhook/scheme.go
+36
-0
No files found.
hack/.golint_failures
View file @
ab053a22
...
...
@@ -551,6 +551,8 @@ staging/src/k8s.io/apiserver/pkg/apis/audit/v1beta1
staging/src/k8s.io/apiserver/pkg/apis/audit/validation
staging/src/k8s.io/apiserver/pkg/apis/example
staging/src/k8s.io/apiserver/pkg/apis/example/v1
staging/src/k8s.io/apiserver/pkg/apis/example2
staging/src/k8s.io/apiserver/pkg/apis/example2/v1
staging/src/k8s.io/apiserver/pkg/audit
staging/src/k8s.io/apiserver/pkg/audit/policy
staging/src/k8s.io/apiserver/pkg/authentication/authenticatorfactory
...
...
hack/local-up-cluster.sh
View file @
ab053a22
...
...
@@ -419,7 +419,7 @@ function start_apiserver {
fi
# Admission Controllers to invoke prior to persisting objects in cluster
ADMISSION_CONTROL
=
Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount
${
security_admission
}
,DefaultStorageClass,DefaultTolerationSeconds,
ResourceQuota,GenericAdmissionWebhook
ADMISSION_CONTROL
=
Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount
${
security_admission
}
,DefaultStorageClass,DefaultTolerationSeconds,
GenericAdmissionWebhook,ResourceQuota
# This is the default dir and filename where the apiserver will generate a self-signed cert
# which should be able to be used as the CA to verify itself
...
...
hack/update-generated-protobuf-dockerized.sh
View file @
ab053a22
...
...
@@ -77,6 +77,7 @@ PACKAGES=(
k8s.io/metrics/pkg/apis/custom_metrics/v1beta1
k8s.io/apiserver/pkg/apis/audit/v1alpha1
k8s.io/apiserver/pkg/apis/audit/v1beta1
k8s.io/apiserver/pkg/apis/example2/v1
)
# requires the 'proto' tag to build (will remove when ready)
...
...
pkg/generated/openapi/BUILD
View file @
ab053a22
...
...
@@ -59,6 +59,7 @@ openapi_library(
"k8s.io/apiserver/pkg/apis/audit/v1alpha1",
"k8s.io/apiserver/pkg/apis/audit/v1beta1",
"k8s.io/apiserver/pkg/apis/example/v1",
"k8s.io/apiserver/pkg/apis/example2/v1",
"k8s.io/client-go/pkg/version",
"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1",
"k8s.io/metrics/pkg/apis/custom_metrics/v1beta1",
...
...
staging/BUILD
View file @
ab053a22
...
...
@@ -96,6 +96,7 @@ filegroup(
"//staging/src/k8s.io/apiserver/pkg/apis/apiserver:all-srcs",
"//staging/src/k8s.io/apiserver/pkg/apis/audit:all-srcs",
"//staging/src/k8s.io/apiserver/pkg/apis/example:all-srcs",
"//staging/src/k8s.io/apiserver/pkg/apis/example2:all-srcs",
"//staging/src/k8s.io/apiserver/pkg/audit:all-srcs",
"//staging/src/k8s.io/apiserver/pkg/authentication/authenticator:all-srcs",
"//staging/src/k8s.io/apiserver/pkg/authentication/authenticatorfactory:all-srcs",
...
...
staging/src/k8s.io/apiextensions-apiserver/test/integration/testserver/resources.go
View file @
ab053a22
...
...
@@ -146,7 +146,11 @@ func NewCurletInstance(namespace, name string) *unstructured.Unstructured {
}
}
func
CreateNewCustomResourceDefinition
(
crd
*
apiextensionsv1beta1
.
CustomResourceDefinition
,
apiExtensionsClient
clientset
.
Interface
,
clientPool
dynamic
.
ClientPool
)
(
dynamic
.
Interface
,
error
)
{
// CreateNewCustomResourceDefinitionWatchUnsafe creates the CRD and makes sure
// the apiextension apiserver has installed the CRD. But it's not safe to watch
// the created CR. Please call CreateNewCustomResourceDefinition if you need to
// watch the CR.
func
CreateNewCustomResourceDefinitionWatchUnsafe
(
crd
*
apiextensionsv1beta1
.
CustomResourceDefinition
,
apiExtensionsClient
clientset
.
Interface
,
clientPool
dynamic
.
ClientPool
)
(
dynamic
.
Interface
,
error
)
{
_
,
err
:=
apiExtensionsClient
.
Apiextensions
()
.
CustomResourceDefinitions
()
.
Create
(
crd
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -169,7 +173,11 @@ func CreateNewCustomResourceDefinition(crd *apiextensionsv1beta1.CustomResourceD
return
nil
,
err
}
dynamicClient
,
err
:=
clientPool
.
ClientForGroupVersionResource
(
schema
.
GroupVersionResource
{
Group
:
crd
.
Spec
.
Group
,
Version
:
crd
.
Spec
.
Version
,
Resource
:
crd
.
Spec
.
Names
.
Plural
})
return
clientPool
.
ClientForGroupVersionResource
(
schema
.
GroupVersionResource
{
Group
:
crd
.
Spec
.
Group
,
Version
:
crd
.
Spec
.
Version
,
Resource
:
crd
.
Spec
.
Names
.
Plural
})
}
func
CreateNewCustomResourceDefinition
(
crd
*
apiextensionsv1beta1
.
CustomResourceDefinition
,
apiExtensionsClient
clientset
.
Interface
,
clientPool
dynamic
.
ClientPool
)
(
dynamic
.
Interface
,
error
)
{
dynamicClient
,
err
:=
CreateNewCustomResourceDefinitionWatchUnsafe
(
crd
,
apiExtensionsClient
,
clientPool
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/BUILD
View file @
ab053a22
...
...
@@ -24,6 +24,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels: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/apimachinery/pkg/runtime/serializer:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
...
...
@@ -46,6 +47,7 @@ go_test(
"admission_test.go",
"authentication_test.go",
"certs_test.go",
"conversion_test.go",
"rules_test.go",
"serviceresolver_test.go",
],
...
...
@@ -58,11 +60,15 @@ go_test(
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels: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/apimachinery/pkg/util/diff:go_default_library",
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example/v1:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example2/v1:go_default_library",
"//vendor/k8s.io/apiserver/pkg/authentication/user:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
"//vendor/k8s.io/client-go/tools/clientcmd/api:go_default_library",
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/admission.go
View file @
ab053a22
...
...
@@ -37,6 +37,7 @@ import (
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
...
...
@@ -134,6 +135,8 @@ type GenericAdmissionWebhook struct {
negotiatedSerializer
runtime
.
NegotiatedSerializer
namespaceLister
corelisters
.
NamespaceLister
client
clientset
.
Interface
convertor
runtime
.
ObjectConvertor
creator
runtime
.
ObjectCreater
authInfoResolver
AuthenticationInfoResolver
cache
*
lru
.
Cache
...
...
@@ -169,6 +172,8 @@ func (a *GenericAdmissionWebhook) SetScheme(scheme *runtime.Scheme) {
a
.
negotiatedSerializer
=
serializer
.
NegotiatedSerializerWrapper
(
runtime
.
SerializerInfo
{
Serializer
:
serializer
.
NewCodecFactory
(
scheme
)
.
LegacyCodec
(
admissionv1alpha1
.
SchemeGroupVersion
),
})
a
.
convertor
=
scheme
a
.
creator
=
scheme
}
}
...
...
@@ -219,6 +224,37 @@ func (a *GenericAdmissionWebhook) loadConfiguration(attr admission.Attributes) (
return
hookConfig
,
nil
}
type
versionedAttributes
struct
{
admission
.
Attributes
oldObject
runtime
.
Object
object
runtime
.
Object
}
func
(
v
versionedAttributes
)
GetObject
()
runtime
.
Object
{
return
v
.
object
}
func
(
v
versionedAttributes
)
GetOldObject
()
runtime
.
Object
{
return
v
.
oldObject
}
func
(
a
*
GenericAdmissionWebhook
)
convertToGVK
(
obj
runtime
.
Object
,
gvk
schema
.
GroupVersionKind
)
(
runtime
.
Object
,
error
)
{
// Unlike other resources, custom resources do not have internal version, so
// if obj is a custom resource, it should not need conversion.
if
obj
.
GetObjectKind
()
.
GroupVersionKind
()
==
gvk
{
return
obj
,
nil
}
out
,
err
:=
a
.
creator
.
New
(
gvk
)
if
err
!=
nil
{
return
nil
,
err
}
err
=
a
.
convertor
.
Convert
(
obj
,
out
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
return
out
,
nil
}
// Admit makes an admission decision based on the request attributes.
func
(
a
*
GenericAdmissionWebhook
)
Admit
(
attr
admission
.
Attributes
)
error
{
hookConfig
,
err
:=
a
.
loadConfiguration
(
attr
)
...
...
@@ -228,14 +264,49 @@ func (a *GenericAdmissionWebhook) Admit(attr admission.Attributes) error {
hooks
:=
hookConfig
.
Webhooks
ctx
:=
context
.
TODO
()
errCh
:=
make
(
chan
error
,
len
(
hooks
))
wg
:=
sync
.
WaitGroup
{}
wg
.
Add
(
len
(
hooks
))
var
relevantHooks
[]
*
v1alpha1
.
Webhook
for
i
:=
range
hooks
{
call
,
err
:=
a
.
shouldCallHook
(
&
hooks
[
i
],
attr
)
if
err
!=
nil
{
return
err
}
if
call
{
relevantHooks
=
append
(
relevantHooks
,
&
hooks
[
i
])
}
}
if
len
(
relevantHooks
)
==
0
{
// no matching hooks
return
nil
}
// convert the object to the external version before sending it to the webhook
versionedAttr
:=
versionedAttributes
{
Attributes
:
attr
,
}
if
oldObj
:=
attr
.
GetOldObject
();
oldObj
!=
nil
{
out
,
err
:=
a
.
convertToGVK
(
oldObj
,
attr
.
GetKind
())
if
err
!=
nil
{
return
apierrors
.
NewInternalError
(
err
)
}
versionedAttr
.
oldObject
=
out
}
if
obj
:=
attr
.
GetObject
();
obj
!=
nil
{
out
,
err
:=
a
.
convertToGVK
(
obj
,
attr
.
GetKind
())
if
err
!=
nil
{
return
apierrors
.
NewInternalError
(
err
)
}
versionedAttr
.
object
=
out
}
wg
:=
sync
.
WaitGroup
{}
errCh
:=
make
(
chan
error
,
len
(
relevantHooks
))
wg
.
Add
(
len
(
relevantHooks
))
for
i
:=
range
relevantHooks
{
go
func
(
hook
*
v1alpha1
.
Webhook
)
{
defer
wg
.
Done
()
err
:=
a
.
callHook
(
ctx
,
hook
,
a
ttr
)
err
:=
a
.
callHook
(
ctx
,
hook
,
versionedA
ttr
)
if
err
==
nil
{
return
}
...
...
@@ -256,7 +327,7 @@ func (a *GenericAdmissionWebhook) Admit(attr admission.Attributes) error {
glog
.
Warningf
(
"rejected by webhook %q: %#v"
,
hook
.
Name
,
err
)
errCh
<-
err
}(
&
h
ooks
[
i
])
}(
relevantH
ooks
[
i
])
}
wg
.
Wait
()
close
(
errCh
)
...
...
@@ -313,7 +384,7 @@ func (a *GenericAdmissionWebhook) getNamespaceLabels(attr admission.Attributes)
// whether the request is exempted by the webhook because of the
// namespaceSelector of the webhook.
func
(
a
*
GenericAdmissionWebhook
)
exemptedByNamespaceSelector
(
h
*
v1alpha1
.
Webhook
,
attr
admission
.
Attributes
)
(
bool
,
e
rror
)
{
func
(
a
*
GenericAdmissionWebhook
)
exemptedByNamespaceSelector
(
h
*
v1alpha1
.
Webhook
,
attr
admission
.
Attributes
)
(
bool
,
*
apierrors
.
StatusE
rror
)
{
namespaceName
:=
attr
.
GetNamespace
()
if
len
(
namespaceName
)
==
0
&&
attr
.
GetResource
()
.
Resource
!=
"namespaces"
{
// If the request is about a cluster scoped resource, and it is not a
...
...
@@ -323,8 +394,14 @@ func (a *GenericAdmissionWebhook) exemptedByNamespaceSelector(h *v1alpha1.Webhoo
return
true
,
nil
}
namespaceLabels
,
err
:=
a
.
getNamespaceLabels
(
attr
)
// this means the namespace is not found, for backwards compatibility,
// return a 404
if
apierrors
.
IsNotFound
(
err
)
{
return
false
,
err
status
,
ok
:=
err
.
(
apierrors
.
APIStatus
)
if
!
ok
{
return
false
,
apierrors
.
NewInternalError
(
err
)
}
return
false
,
&
apierrors
.
StatusError
{
status
.
Status
()}
}
if
err
!=
nil
{
return
false
,
apierrors
.
NewInternalError
(
err
)
...
...
@@ -337,15 +414,8 @@ func (a *GenericAdmissionWebhook) exemptedByNamespaceSelector(h *v1alpha1.Webhoo
return
!
selector
.
Matches
(
labels
.
Set
(
namespaceLabels
)),
nil
}
func
(
a
*
GenericAdmissionWebhook
)
callHook
(
ctx
context
.
Context
,
h
*
v1alpha1
.
Webhook
,
attr
admission
.
Attributes
)
error
{
excluded
,
err
:=
a
.
exemptedByNamespaceSelector
(
h
,
attr
)
if
err
!=
nil
{
return
err
}
if
excluded
{
return
nil
}
matches
:=
false
func
(
a
*
GenericAdmissionWebhook
)
shouldCallHook
(
h
*
v1alpha1
.
Webhook
,
attr
admission
.
Attributes
)
(
bool
,
*
apierrors
.
StatusError
)
{
var
matches
bool
for
_
,
r
:=
range
h
.
Rules
{
m
:=
RuleMatcher
{
Rule
:
r
,
Attr
:
attr
}
if
m
.
Matches
()
{
...
...
@@ -354,9 +424,17 @@ func (a *GenericAdmissionWebhook) callHook(ctx context.Context, h *v1alpha1.Webh
}
}
if
!
matches
{
return
nil
return
false
,
nil
}
excluded
,
err
:=
a
.
exemptedByNamespaceSelector
(
h
,
attr
)
if
err
!=
nil
{
return
false
,
err
}
return
!
excluded
,
nil
}
func
(
a
*
GenericAdmissionWebhook
)
callHook
(
ctx
context
.
Context
,
h
*
v1alpha1
.
Webhook
,
attr
admission
.
Attributes
)
error
{
// Make the webhook request
request
:=
createAdmissionReview
(
attr
)
client
,
err
:=
a
.
hookClient
(
h
)
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/conversion_test.go
0 → 100644
View file @
ab053a22
/*
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
webhook
import
(
"reflect"
"testing"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/apis/example"
examplev1
"k8s.io/apiserver/pkg/apis/example/v1"
example2v1
"k8s.io/apiserver/pkg/apis/example2/v1"
)
func
initiateScheme
()
*
runtime
.
Scheme
{
s
:=
runtime
.
NewScheme
()
example
.
AddToScheme
(
s
)
examplev1
.
AddToScheme
(
s
)
example2v1
.
AddToScheme
(
s
)
return
s
}
func
TestConvertToGVK
(
t
*
testing
.
T
)
{
scheme
:=
initiateScheme
()
w
:=
GenericAdmissionWebhook
{
convertor
:
scheme
,
creator
:
scheme
,
}
table
:=
map
[
string
]
struct
{
obj
runtime
.
Object
gvk
schema
.
GroupVersionKind
expectedObj
runtime
.
Object
}{
"convert example#Pod to example/v1#Pod"
:
{
obj
:
&
example
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"pod1"
,
Labels
:
map
[
string
]
string
{
"key"
:
"value"
,
},
},
Spec
:
example
.
PodSpec
{
RestartPolicy
:
example
.
RestartPolicy
(
"never"
),
},
},
gvk
:
examplev1
.
SchemeGroupVersion
.
WithKind
(
"Pod"
),
expectedObj
:
&
examplev1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"pod1"
,
Labels
:
map
[
string
]
string
{
"key"
:
"value"
,
},
},
Spec
:
examplev1
.
PodSpec
{
RestartPolicy
:
examplev1
.
RestartPolicy
(
"never"
),
},
},
},
"convert example#replicaset to example2/v1#replicaset"
:
{
obj
:
&
example
.
ReplicaSet
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"rs1"
,
Labels
:
map
[
string
]
string
{
"key"
:
"value"
,
},
},
Spec
:
example
.
ReplicaSetSpec
{
Replicas
:
1
,
},
},
gvk
:
example2v1
.
SchemeGroupVersion
.
WithKind
(
"ReplicaSet"
),
expectedObj
:
&
example2v1
.
ReplicaSet
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"rs1"
,
Labels
:
map
[
string
]
string
{
"key"
:
"value"
,
},
},
Spec
:
example2v1
.
ReplicaSetSpec
{
Replicas
:
func
()
*
int32
{
var
i
int32
;
i
=
1
;
return
&
i
}(),
},
},
},
"no conversion for Unstructured object whose gvk matches the desired gvk"
:
{
obj
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"apiVersion"
:
"mygroup.k8s.io/v1"
,
"kind"
:
"Flunder"
,
"data"
:
map
[
string
]
interface
{}{
"Key"
:
"Value"
,
},
},
},
gvk
:
schema
.
GroupVersionKind
{
Group
:
"mygroup.k8s.io"
,
Version
:
"v1"
,
Kind
:
"Flunder"
},
expectedObj
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"apiVersion"
:
"mygroup.k8s.io/v1"
,
"kind"
:
"Flunder"
,
"data"
:
map
[
string
]
interface
{}{
"Key"
:
"Value"
,
},
},
},
},
}
for
name
,
test
:=
range
table
{
t
.
Run
(
name
,
func
(
t
*
testing
.
T
)
{
actual
,
err
:=
w
.
convertToGVK
(
test
.
obj
,
test
.
gvk
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
!
reflect
.
DeepEqual
(
actual
,
test
.
expectedObj
)
{
t
.
Errorf
(
"
\n
expected:
\n
%#v
\n
got:
\n
%#v
\n
"
,
test
.
expectedObj
,
actual
)
}
})
}
}
staging/src/k8s.io/apiserver/pkg/apis/example/install/install.go
View file @
ab053a22
...
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package install installs the
certificates
API group, making it available as
// Package install installs the
example
API group, making it available as
// an option to all of the API encoding/decoding machinery.
package
install
...
...
staging/src/k8s.io/apiserver/pkg/apis/example/register.go
View file @
ab053a22
...
...
@@ -46,6 +46,7 @@ func Resource(resource string) schema.GroupResource {
func
addKnownTypes
(
scheme
*
runtime
.
Scheme
)
error
{
scheme
.
AddKnownTypes
(
SchemeGroupVersion
,
&
Pod
{},
&
ReplicaSet
{},
)
return
nil
}
staging/src/k8s.io/apiserver/pkg/apis/example/types.go
View file @
ab053a22
...
...
@@ -136,3 +136,35 @@ type PodList struct {
Items
[]
Pod
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// ReplicaSet ensures that a specified number of pod replicas are running at any given time.
type
ReplicaSet
struct
{
metav1
.
TypeMeta
// +optional
metav1
.
ObjectMeta
// Spec defines the desired behavior of this ReplicaSet.
// +optional
Spec
ReplicaSetSpec
// Status is the current status of this ReplicaSet. This data may be
// out of date by some window of time.
// +optional
Status
ReplicaSetStatus
}
// ReplicaSetSpec is the specification of a ReplicaSet.
// As the internal representation of a ReplicaSet, it must have
// a Template set.
type
ReplicaSetSpec
struct
{
// Replicas is the number of desired replicas.
Replicas
int32
}
// ReplicaSetStatus represents the current status of a ReplicaSet.
type
ReplicaSetStatus
struct
{
// Replicas is the number of actual replicas.
Replicas
int32
}
staging/src/k8s.io/apiserver/pkg/apis/example/zz_generated.deepcopy.go
View file @
ab053a22
...
...
@@ -178,3 +178,64 @@ func (in *PodStatus) DeepCopy() *PodStatus {
in
.
DeepCopyInto
(
out
)
return
out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func
(
in
*
ReplicaSet
)
DeepCopyInto
(
out
*
ReplicaSet
)
{
*
out
=
*
in
out
.
TypeMeta
=
in
.
TypeMeta
in
.
ObjectMeta
.
DeepCopyInto
(
&
out
.
ObjectMeta
)
out
.
Spec
=
in
.
Spec
out
.
Status
=
in
.
Status
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReplicaSet.
func
(
in
*
ReplicaSet
)
DeepCopy
()
*
ReplicaSet
{
if
in
==
nil
{
return
nil
}
out
:=
new
(
ReplicaSet
)
in
.
DeepCopyInto
(
out
)
return
out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func
(
in
*
ReplicaSet
)
DeepCopyObject
()
runtime
.
Object
{
if
c
:=
in
.
DeepCopy
();
c
!=
nil
{
return
c
}
else
{
return
nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func
(
in
*
ReplicaSetSpec
)
DeepCopyInto
(
out
*
ReplicaSetSpec
)
{
*
out
=
*
in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReplicaSetSpec.
func
(
in
*
ReplicaSetSpec
)
DeepCopy
()
*
ReplicaSetSpec
{
if
in
==
nil
{
return
nil
}
out
:=
new
(
ReplicaSetSpec
)
in
.
DeepCopyInto
(
out
)
return
out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func
(
in
*
ReplicaSetStatus
)
DeepCopyInto
(
out
*
ReplicaSetStatus
)
{
*
out
=
*
in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReplicaSetStatus.
func
(
in
*
ReplicaSetStatus
)
DeepCopy
()
*
ReplicaSetStatus
{
if
in
==
nil
{
return
nil
}
out
:=
new
(
ReplicaSetStatus
)
in
.
DeepCopyInto
(
out
)
return
out
}
staging/src/k8s.io/apiserver/pkg/apis/example2/BUILD
0 → 100644
View file @
ab053a22
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"doc.go",
"register.go",
"zz_generated.deepcopy.go",
],
importpath = "k8s.io/apiserver/pkg/apis/example2",
deps = [
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//staging/src/k8s.io/apiserver/pkg/apis/example2/install:all-srcs",
"//staging/src/k8s.io/apiserver/pkg/apis/example2/v1:all-srcs",
],
tags = ["automanaged"],
)
staging/src/k8s.io/apiserver/pkg/apis/example2/doc.go
0 → 100644
View file @
ab053a22
/*
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.
*/
// +k8s:deepcopy-gen=package
// +groupName=example2.k8s.io
//
// package example2 contains an example API whose internal version is defined in
// another group ("example"). This happens if a type is moved to a different
// group. It's not recommended to move types across groups, though Kubernetes
// have a few cases due to historical reasons. This package is for tests.
package
example2
// import "k8s.io/apiserver/pkg/apis/example2"
staging/src/k8s.io/apiserver/pkg/apis/example2/install/BUILD
0 → 100644
View file @
ab053a22
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["install.go"],
importpath = "k8s.io/apiserver/pkg/apis/example2/install",
deps = [
"//vendor/k8s.io/apimachinery/pkg/apimachinery/announced:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apimachinery/registered:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example2:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example2/v1:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["roundtrip_test.go"],
importpath = "k8s.io/apiserver/pkg/apis/example2/install",
library = ":go_default_library",
deps = [
"//vendor/k8s.io/apimachinery/pkg/api/testing/roundtrip:go_default_library",
"//vendor/k8s.io/apiserver/pkg/apis/example/fuzzer:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
staging/src/k8s.io/apiserver/pkg/apis/example2/install/install.go
0 → 100644
View file @
ab053a22
/*
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 install installs the example2 API group, making it available as
// an option to all of the API encoding/decoding machinery.
package
install
import
(
"k8s.io/apimachinery/pkg/apimachinery/announced"
"k8s.io/apimachinery/pkg/apimachinery/registered"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/apis/example"
"k8s.io/apiserver/pkg/apis/example2"
example2v1
"k8s.io/apiserver/pkg/apis/example2/v1"
)
// Install registers the API group and adds types to a scheme
func
Install
(
groupFactoryRegistry
announced
.
APIGroupFactoryRegistry
,
registry
*
registered
.
APIRegistrationManager
,
scheme
*
runtime
.
Scheme
)
{
if
err
:=
announced
.
NewGroupMetaFactory
(
&
announced
.
GroupMetaFactoryArgs
{
GroupName
:
example2
.
GroupName
,
VersionPreferenceOrder
:
[]
string
{
example2v1
.
SchemeGroupVersion
.
Version
},
AddInternalObjectsToScheme
:
example
.
AddToScheme
,
},
announced
.
VersionToSchemeFunc
{
example2v1
.
SchemeGroupVersion
.
Version
:
example2v1
.
AddToScheme
,
},
)
.
Announce
(
groupFactoryRegistry
)
.
RegisterAndEnable
(
registry
,
scheme
);
err
!=
nil
{
panic
(
err
)
}
}
staging/src/k8s.io/apiserver/pkg/apis/example2/install/roundtrip_test.go
0 → 100644
View file @
ab053a22
/*
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
install
import
(
"testing"
"k8s.io/apimachinery/pkg/api/testing/roundtrip"
examplefuzzer
"k8s.io/apiserver/pkg/apis/example/fuzzer"
)
func
TestRoundTrip
(
t
*
testing
.
T
)
{
roundtrip
.
RoundTripTestForAPIGroup
(
t
,
Install
,
examplefuzzer
.
Funcs
)
}
staging/src/k8s.io/apiserver/pkg/apis/example2/register.go
0 → 100644
View file @
ab053a22
/*
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
example2
import
(
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/apis/example"
)
var
(
SchemeBuilder
=
runtime
.
NewSchemeBuilder
(
addKnownTypes
)
AddToScheme
=
SchemeBuilder
.
AddToScheme
)
// GroupName is the group name use in this package
const
GroupName
=
"example2.apiserver.k8s.io"
// SchemeGroupVersion is group version used to register these objects
var
SchemeGroupVersion
=
schema
.
GroupVersion
{
Group
:
GroupName
,
Version
:
runtime
.
APIVersionInternal
}
// Kind takes an unqualified kind and returns a Group qualified GroupKind
func
Kind
(
kind
string
)
schema
.
GroupKind
{
return
SchemeGroupVersion
.
WithKind
(
kind
)
.
GroupKind
()
}
// Resource takes an unqualified resource and returns a Group qualified GroupResource
func
Resource
(
resource
string
)
schema
.
GroupResource
{
return
SchemeGroupVersion
.
WithResource
(
resource
)
.
GroupResource
()
}
// Adds the list of known types to the given scheme.
func
addKnownTypes
(
scheme
*
runtime
.
Scheme
)
error
{
scheme
.
AddKnownTypes
(
SchemeGroupVersion
,
&
example
.
ReplicaSet
{},
)
return
nil
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/BUILD
0 → 100644
View file @
ab053a22
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"conversion.go",
"defaults.go",
"doc.go",
"generated.pb.go",
"register.go",
"types.go",
"types_swagger_doc_generated.go",
"zz_generated.conversion.go",
"zz_generated.deepcopy.go",
"zz_generated.defaults.go",
],
importpath = "k8s.io/apiserver/pkg/apis/example2/v1",
deps = [
"//vendor/github.com/gogo/protobuf/proto:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/conversion: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/apis/example:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
filegroup(
name = "go_default_library_protos",
srcs = ["generated.proto"],
visibility = ["//visibility:public"],
)
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/conversion.go
0 → 100644
View file @
ab053a22
/*
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
v1
import
(
conversion
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
example
"k8s.io/apiserver/pkg/apis/example"
)
func
addConversionFuncs
(
scheme
*
runtime
.
Scheme
)
error
{
// Add non-generated conversion functions to handle the *int32 -> int32
// conversion. A pointer is useful in the versioned type so we can default
// it, but a plain int32 is more convenient in the internal type. These
// functions are the same as the autogenerated ones in every other way.
err
:=
scheme
.
AddConversionFuncs
(
Convert_example_ReplicaSetSpec_To_v1_ReplicaSetSpec
,
Convert_v1_ReplicaSetSpec_To_example_ReplicaSetSpec
,
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
Convert_example_ReplicaSetSpec_To_v1_ReplicaSetSpec
(
in
*
example
.
ReplicaSetSpec
,
out
*
ReplicaSetSpec
,
s
conversion
.
Scope
)
error
{
out
.
Replicas
=
new
(
int32
)
*
out
.
Replicas
=
int32
(
in
.
Replicas
)
return
nil
}
func
Convert_v1_ReplicaSetSpec_To_example_ReplicaSetSpec
(
in
*
ReplicaSetSpec
,
out
*
example
.
ReplicaSetSpec
,
s
conversion
.
Scope
)
error
{
if
in
.
Replicas
!=
nil
{
out
.
Replicas
=
*
in
.
Replicas
}
return
nil
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/defaults.go
0 → 100644
View file @
ab053a22
/*
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
v1
import
(
"k8s.io/apimachinery/pkg/runtime"
)
func
addDefaultingFuncs
(
scheme
*
runtime
.
Scheme
)
error
{
// return RegisterDefaults(scheme)
return
nil
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/doc.go
0 → 100644
View file @
ab053a22
/*
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.
*/
// +k8s:deepcopy-gen=package
// +k8s:conversion-gen=k8s.io/apiserver/pkg/apis/example2
// +k8s:conversion-gen=k8s.io/apiserver/pkg/apis/example
// +k8s:openapi-gen=false
// +k8s:defaulter-gen=TypeMeta
// +groupName=example2.apiserver.k8s.io
package
v1
// import "k8s.io/apiserver/pkg/apis/example2/v1"
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/generated.pb.go
0 → 100644
View file @
ab053a22
This diff is collapsed.
Click to expand it.
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/generated.proto
0 → 100644
View file @
ab053a22
/*
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.
*/
// This file was autogenerated by go-to-protobuf. Do not edit it manually!
syntax
=
'proto2'
;
package
k8s
.
io.apiserver.pkg.apis.example2.v1
;
import
"k8s.io/api/core/v1/generated.proto"
;
import
"k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto"
;
import
"k8s.io/apimachinery/pkg/runtime/generated.proto"
;
import
"k8s.io/apimachinery/pkg/runtime/schema/generated.proto"
;
import
"k8s.io/apimachinery/pkg/util/intstr/generated.proto"
;
// Package-wide variables from generator "generated".
option
go_package
=
"v1"
;
// ReplicaSet ensures that a specified number of pod replicas are running at any given time.
message
ReplicaSet
{
// If the Labels of a ReplicaSet are empty, they are defaulted to
// be the same as the Pod(s) that the ReplicaSet manages.
// Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
optional
k8s.io.apimachinery.pkg.apis.meta.v1.ObjectMeta
metadata
=
1
;
// Spec defines the specification of the desired behavior of the ReplicaSet.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
// +optional
optional
ReplicaSetSpec
spec
=
2
;
// Status is the most recently observed status of the ReplicaSet.
// This data may be out of date by some window of time.
// Populated by the system.
// Read-only.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
// +optional
optional
ReplicaSetStatus
status
=
3
;
}
// ReplicaSetSpec is the specification of a ReplicaSet.
message
ReplicaSetSpec
{
// Replicas is the number of desired replicas.
// This is a pointer to distinguish between explicit zero and unspecified.
// Defaults to 1.
// More info: https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
// +optional
optional
int32
replicas
=
1
;
}
// ReplicaSetStatus represents the current status of a ReplicaSet.
message
ReplicaSetStatus
{
// Replicas is the most recently oberved number of replicas.
// More info: https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
optional
int32
replicas
=
1
;
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/register.go
0 → 100644
View file @
ab053a22
/*
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
v1
import
(
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// GroupName is the group name use in this package
const
GroupName
=
"example2.apiserver.k8s.io"
// SchemeGroupVersion is group version used to register these objects
var
SchemeGroupVersion
=
schema
.
GroupVersion
{
Group
:
GroupName
,
Version
:
"v1"
}
// Kind takes an unqualified kind and returns a Group qualified GroupKind
func
Kind
(
kind
string
)
schema
.
GroupKind
{
return
SchemeGroupVersion
.
WithKind
(
kind
)
.
GroupKind
()
}
// Resource takes an unqualified resource and returns a Group qualified GroupResource
func
Resource
(
resource
string
)
schema
.
GroupResource
{
return
SchemeGroupVersion
.
WithResource
(
resource
)
.
GroupResource
()
}
var
(
// TODO: move SchemeBuilder with zz_generated.deepcopy.go to k8s.io/api.
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
SchemeBuilder
runtime
.
SchemeBuilder
localSchemeBuilder
=
&
SchemeBuilder
AddToScheme
=
localSchemeBuilder
.
AddToScheme
)
func
init
()
{
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
localSchemeBuilder
.
Register
(
addKnownTypes
,
addConversionFuncs
,
addDefaultingFuncs
)
}
// Adds the list of known types to the given scheme.
func
addKnownTypes
(
scheme
*
runtime
.
Scheme
)
error
{
scheme
.
AddKnownTypes
(
SchemeGroupVersion
,
&
ReplicaSet
{},
)
metav1
.
AddToGroupVersion
(
scheme
,
SchemeGroupVersion
)
return
nil
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/types.go
0 → 100644
View file @
ab053a22
/*
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
v1
import
(
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// ReplicaSet ensures that a specified number of pod replicas are running at any given time.
type
ReplicaSet
struct
{
metav1
.
TypeMeta
`json:",inline"`
// If the Labels of a ReplicaSet are empty, they are defaulted to
// be the same as the Pod(s) that the ReplicaSet manages.
// Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
metav1
.
ObjectMeta
`json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Spec defines the specification of the desired behavior of the ReplicaSet.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
// +optional
Spec
ReplicaSetSpec
`json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
// Status is the most recently observed status of the ReplicaSet.
// This data may be out of date by some window of time.
// Populated by the system.
// Read-only.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
// +optional
Status
ReplicaSetStatus
`json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}
// ReplicaSetSpec is the specification of a ReplicaSet.
type
ReplicaSetSpec
struct
{
// Replicas is the number of desired replicas.
// This is a pointer to distinguish between explicit zero and unspecified.
// Defaults to 1.
// More info: https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
// +optional
Replicas
*
int32
`json:"replicas,omitempty" protobuf:"varint,1,opt,name=replicas"`
}
// ReplicaSetStatus represents the current status of a ReplicaSet.
type
ReplicaSetStatus
struct
{
// Replicas is the most recently oberved number of replicas.
// More info: https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
Replicas
int32
`json:"replicas" protobuf:"varint,1,opt,name=replicas"`
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/types_swagger_doc_generated.go
0 → 100644
View file @
ab053a22
/*
Copyright 2016 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
v1
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/zz_generated.conversion.go
0 → 100644
View file @
ab053a22
// +build !ignore_autogenerated
/*
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.
*/
// This file was autogenerated by conversion-gen. Do not edit it manually!
package
v1
import
(
meta_v1
"k8s.io/apimachinery/pkg/apis/meta/v1"
conversion
"k8s.io/apimachinery/pkg/conversion"
runtime
"k8s.io/apimachinery/pkg/runtime"
example
"k8s.io/apiserver/pkg/apis/example"
)
func
init
()
{
localSchemeBuilder
.
Register
(
RegisterConversions
)
}
// RegisterConversions adds conversion functions to the given scheme.
// Public to allow building arbitrary schemes.
func
RegisterConversions
(
scheme
*
runtime
.
Scheme
)
error
{
return
scheme
.
AddGeneratedConversionFuncs
(
Convert_v1_ReplicaSet_To_example_ReplicaSet
,
Convert_example_ReplicaSet_To_v1_ReplicaSet
,
Convert_v1_ReplicaSetSpec_To_example_ReplicaSetSpec
,
Convert_example_ReplicaSetSpec_To_v1_ReplicaSetSpec
,
Convert_v1_ReplicaSetStatus_To_example_ReplicaSetStatus
,
Convert_example_ReplicaSetStatus_To_v1_ReplicaSetStatus
,
)
}
func
autoConvert_v1_ReplicaSet_To_example_ReplicaSet
(
in
*
ReplicaSet
,
out
*
example
.
ReplicaSet
,
s
conversion
.
Scope
)
error
{
out
.
ObjectMeta
=
in
.
ObjectMeta
if
err
:=
Convert_v1_ReplicaSetSpec_To_example_ReplicaSetSpec
(
&
in
.
Spec
,
&
out
.
Spec
,
s
);
err
!=
nil
{
return
err
}
if
err
:=
Convert_v1_ReplicaSetStatus_To_example_ReplicaSetStatus
(
&
in
.
Status
,
&
out
.
Status
,
s
);
err
!=
nil
{
return
err
}
return
nil
}
// Convert_v1_ReplicaSet_To_example_ReplicaSet is an autogenerated conversion function.
func
Convert_v1_ReplicaSet_To_example_ReplicaSet
(
in
*
ReplicaSet
,
out
*
example
.
ReplicaSet
,
s
conversion
.
Scope
)
error
{
return
autoConvert_v1_ReplicaSet_To_example_ReplicaSet
(
in
,
out
,
s
)
}
func
autoConvert_example_ReplicaSet_To_v1_ReplicaSet
(
in
*
example
.
ReplicaSet
,
out
*
ReplicaSet
,
s
conversion
.
Scope
)
error
{
out
.
ObjectMeta
=
in
.
ObjectMeta
if
err
:=
Convert_example_ReplicaSetSpec_To_v1_ReplicaSetSpec
(
&
in
.
Spec
,
&
out
.
Spec
,
s
);
err
!=
nil
{
return
err
}
if
err
:=
Convert_example_ReplicaSetStatus_To_v1_ReplicaSetStatus
(
&
in
.
Status
,
&
out
.
Status
,
s
);
err
!=
nil
{
return
err
}
return
nil
}
// Convert_example_ReplicaSet_To_v1_ReplicaSet is an autogenerated conversion function.
func
Convert_example_ReplicaSet_To_v1_ReplicaSet
(
in
*
example
.
ReplicaSet
,
out
*
ReplicaSet
,
s
conversion
.
Scope
)
error
{
return
autoConvert_example_ReplicaSet_To_v1_ReplicaSet
(
in
,
out
,
s
)
}
func
autoConvert_v1_ReplicaSetSpec_To_example_ReplicaSetSpec
(
in
*
ReplicaSetSpec
,
out
*
example
.
ReplicaSetSpec
,
s
conversion
.
Scope
)
error
{
if
err
:=
meta_v1
.
Convert_Pointer_int32_To_int32
(
&
in
.
Replicas
,
&
out
.
Replicas
,
s
);
err
!=
nil
{
return
err
}
return
nil
}
func
autoConvert_example_ReplicaSetSpec_To_v1_ReplicaSetSpec
(
in
*
example
.
ReplicaSetSpec
,
out
*
ReplicaSetSpec
,
s
conversion
.
Scope
)
error
{
if
err
:=
meta_v1
.
Convert_int32_To_Pointer_int32
(
&
in
.
Replicas
,
&
out
.
Replicas
,
s
);
err
!=
nil
{
return
err
}
return
nil
}
func
autoConvert_v1_ReplicaSetStatus_To_example_ReplicaSetStatus
(
in
*
ReplicaSetStatus
,
out
*
example
.
ReplicaSetStatus
,
s
conversion
.
Scope
)
error
{
out
.
Replicas
=
in
.
Replicas
return
nil
}
// Convert_v1_ReplicaSetStatus_To_example_ReplicaSetStatus is an autogenerated conversion function.
func
Convert_v1_ReplicaSetStatus_To_example_ReplicaSetStatus
(
in
*
ReplicaSetStatus
,
out
*
example
.
ReplicaSetStatus
,
s
conversion
.
Scope
)
error
{
return
autoConvert_v1_ReplicaSetStatus_To_example_ReplicaSetStatus
(
in
,
out
,
s
)
}
func
autoConvert_example_ReplicaSetStatus_To_v1_ReplicaSetStatus
(
in
*
example
.
ReplicaSetStatus
,
out
*
ReplicaSetStatus
,
s
conversion
.
Scope
)
error
{
out
.
Replicas
=
in
.
Replicas
return
nil
}
// Convert_example_ReplicaSetStatus_To_v1_ReplicaSetStatus is an autogenerated conversion function.
func
Convert_example_ReplicaSetStatus_To_v1_ReplicaSetStatus
(
in
*
example
.
ReplicaSetStatus
,
out
*
ReplicaSetStatus
,
s
conversion
.
Scope
)
error
{
return
autoConvert_example_ReplicaSetStatus_To_v1_ReplicaSetStatus
(
in
,
out
,
s
)
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/zz_generated.deepcopy.go
0 → 100644
View file @
ab053a22
// +build !ignore_autogenerated
/*
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.
*/
// This file was autogenerated by deepcopy-gen. Do not edit it manually!
package
v1
import
(
runtime
"k8s.io/apimachinery/pkg/runtime"
)
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func
(
in
*
ReplicaSet
)
DeepCopyInto
(
out
*
ReplicaSet
)
{
*
out
=
*
in
out
.
TypeMeta
=
in
.
TypeMeta
in
.
ObjectMeta
.
DeepCopyInto
(
&
out
.
ObjectMeta
)
in
.
Spec
.
DeepCopyInto
(
&
out
.
Spec
)
out
.
Status
=
in
.
Status
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReplicaSet.
func
(
in
*
ReplicaSet
)
DeepCopy
()
*
ReplicaSet
{
if
in
==
nil
{
return
nil
}
out
:=
new
(
ReplicaSet
)
in
.
DeepCopyInto
(
out
)
return
out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func
(
in
*
ReplicaSet
)
DeepCopyObject
()
runtime
.
Object
{
if
c
:=
in
.
DeepCopy
();
c
!=
nil
{
return
c
}
else
{
return
nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func
(
in
*
ReplicaSetSpec
)
DeepCopyInto
(
out
*
ReplicaSetSpec
)
{
*
out
=
*
in
if
in
.
Replicas
!=
nil
{
in
,
out
:=
&
in
.
Replicas
,
&
out
.
Replicas
if
*
in
==
nil
{
*
out
=
nil
}
else
{
*
out
=
new
(
int32
)
**
out
=
**
in
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReplicaSetSpec.
func
(
in
*
ReplicaSetSpec
)
DeepCopy
()
*
ReplicaSetSpec
{
if
in
==
nil
{
return
nil
}
out
:=
new
(
ReplicaSetSpec
)
in
.
DeepCopyInto
(
out
)
return
out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func
(
in
*
ReplicaSetStatus
)
DeepCopyInto
(
out
*
ReplicaSetStatus
)
{
*
out
=
*
in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReplicaSetStatus.
func
(
in
*
ReplicaSetStatus
)
DeepCopy
()
*
ReplicaSetStatus
{
if
in
==
nil
{
return
nil
}
out
:=
new
(
ReplicaSetStatus
)
in
.
DeepCopyInto
(
out
)
return
out
}
staging/src/k8s.io/apiserver/pkg/apis/example2/v1/zz_generated.defaults.go
0 → 100644
View file @
ab053a22
// +build !ignore_autogenerated
/*
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.
*/
// This file was autogenerated by defaulter-gen. Do not edit it manually!
package
v1
import
(
runtime
"k8s.io/apimachinery/pkg/runtime"
)
// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func
RegisterDefaults
(
scheme
*
runtime
.
Scheme
)
error
{
return
nil
}
staging/src/k8s.io/apiserver/pkg/apis/example2/zz_generated.deepcopy.go
0 → 100644
View file @
ab053a22
// +build !ignore_autogenerated
/*
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.
*/
// This file was autogenerated by deepcopy-gen. Do not edit it manually!
package
example2
test/e2e/apimachinery/BUILD
View file @
ab053a22
...
...
@@ -60,6 +60,7 @@ go_library(
"//vendor/k8s.io/apiserver/pkg/authentication/user:go_default_library",
"//vendor/k8s.io/apiserver/pkg/storage/names:go_default_library",
"//vendor/k8s.io/client-go/discovery:go_default_library",
"//vendor/k8s.io/client-go/dynamic:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/k8s.io/client-go/util/cert:go_default_library",
"//vendor/k8s.io/client-go/util/retry:go_default_library",
...
...
test/e2e/apimachinery/webhook.go
View file @
ab053a22
This diff is collapsed.
Click to expand it.
test/images/webhook/BUILD
View file @
ab053a22
...
...
@@ -5,14 +5,18 @@ go_library(
srcs = [
"config.go",
"main.go",
"scheme.go",
],
importpath = "k8s.io/kubernetes/test/images/webhook",
visibility = ["//visibility:private"],
deps = [
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/k8s.io/api/admission/v1alpha1:go_default_library",
"//vendor/k8s.io/api/admissionregistration/v1alpha1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//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/serializer:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
],
...
...
test/images/webhook/Makefile
View file @
ab053a22
...
...
@@ -14,6 +14,7 @@
build
:
CGO_ENABLED
=
0
GOOS
=
linux go build
-a
-installsuffix
cgo
-o
webhook .
docker build
--no-cache
-t
gcr.io/kubernetes-e2e-test-images/k8s-sample-admission-webhook-amd64:1.8v2 .
docker build
--no-cache
-t
gcr.io/kubernetes-e2e-test-images/k8s-sample-admission-webhook-amd64:1.8v3 .
rm
-rf
webhook
push
:
gcloud docker
--
push gcr.io/kubernetes-e2e-test-images/k8s-sample-admission-webhook-amd64:1.8v
2
gcloud docker
--
push gcr.io/kubernetes-e2e-test-images/k8s-sample-admission-webhook-amd64:1.8v
3
test/images/webhook/main.go
View file @
ab053a22
...
...
@@ -19,13 +19,14 @@ package main
import
(
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/golang/glog"
"k8s.io/api/admission/v1alpha1"
"k8s.io/api/core/v1"
corev1
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
)
...
...
@@ -43,58 +44,56 @@ func (c *Config) addFlags() {
"File containing the default x509 private key matching --tls-cert-file."
)
}
func
toAdmissionReviewStatus
(
err
error
)
*
v1alpha1
.
AdmissionReviewStatus
{
return
&
v1alpha1
.
AdmissionReviewStatus
{
Result
:
&
metav1
.
Status
{
Message
:
err
.
Error
(),
},
}
}
// only allow pods to pull images from specific registry.
func
admitPods
(
data
[]
byte
)
*
v1alpha1
.
AdmissionReviewStatus
{
func
admitPods
(
ar
v1alpha1
.
AdmissionReview
)
*
v1alpha1
.
AdmissionReviewStatus
{
glog
.
V
(
2
)
.
Info
(
"admitting pods"
)
ar
:=
v1alpha1
.
AdmissionReview
{}
if
err
:=
json
.
Unmarshal
(
data
,
&
ar
);
err
!=
nil
{
glog
.
Error
(
err
)
return
nil
}
podResource
:=
metav1
.
GroupVersionResource
{
Group
:
""
,
Version
:
"v1"
,
Resource
:
"pods"
}
if
ar
.
Spec
.
Resource
!=
podResource
{
glog
.
Errorf
(
"expect resource to be %s"
,
podResource
)
return
nil
err
:=
fmt
.
Errorf
(
"expect resource to be %s"
,
podResource
)
glog
.
Error
(
err
)
return
toAdmissionReviewStatus
(
err
)
}
raw
:=
ar
.
Spec
.
Object
.
Raw
pod
:=
v1
.
Pod
{}
if
err
:=
json
.
Unmarshal
(
raw
,
&
pod
);
err
!=
nil
{
pod
:=
corev1
.
Pod
{}
deserializer
:=
codecs
.
UniversalDeserializer
()
if
_
,
_
,
err
:=
deserializer
.
Decode
(
raw
,
nil
,
&
pod
);
err
!=
nil
{
glog
.
Error
(
err
)
return
nil
return
toAdmissionReviewStatus
(
err
)
}
reviewStatus
:=
v1alpha1
.
AdmissionReviewStatus
{}
reviewStatus
.
Allowed
=
true
// Note: the apiserver encodes the api.Pod. Decoding it as a v1.Pod will
// lose the metadata. So the following check on labels will not work
// until we let the apiserver encodes the versioned object.
var
msg
string
for
k
,
v
:=
range
pod
.
Labels
{
if
k
==
"webhook-e2e-test"
&&
v
==
"webhook-disallow"
{
reviewStatus
.
Allowed
=
false
reviewStatus
.
Result
=
&
metav1
.
Status
{
Reason
:
"the pod contains unwanted label"
,
}
msg
=
msg
+
"the pod contains unwanted label; "
}
}
for
_
,
container
:=
range
pod
.
Spec
.
Containers
{
if
strings
.
Contains
(
container
.
Name
,
"webhook-disallow"
)
{
reviewStatus
.
Allowed
=
false
reviewStatus
.
Result
=
&
metav1
.
Status
{
Message
:
"the pod contains unwanted container name"
,
}
msg
=
msg
+
"the pod contains unwanted container name; "
}
}
if
!
reviewStatus
.
Allowed
{
reviewStatus
.
Result
=
&
metav1
.
Status
{
Message
:
strings
.
TrimSpace
(
msg
)}
}
return
&
reviewStatus
}
// deny configmaps with specific key-value pair.
func
admitConfigMaps
(
data
[]
byte
)
*
v1alpha1
.
AdmissionReviewStatus
{
func
admitConfigMaps
(
ar
v1alpha1
.
AdmissionReview
)
*
v1alpha1
.
AdmissionReviewStatus
{
glog
.
V
(
2
)
.
Info
(
"admitting configmaps"
)
ar
:=
v1alpha1
.
AdmissionReview
{}
if
err
:=
json
.
Unmarshal
(
data
,
&
ar
);
err
!=
nil
{
glog
.
Error
(
err
)
return
nil
}
configMapResource
:=
metav1
.
GroupVersionResource
{
Group
:
""
,
Version
:
"v1"
,
Resource
:
"configmaps"
}
if
ar
.
Spec
.
Resource
!=
configMapResource
{
glog
.
Errorf
(
"expect resource to be %s"
,
configMapResource
)
...
...
@@ -102,10 +101,11 @@ func admitConfigMaps(data []byte) *v1alpha1.AdmissionReviewStatus {
}
raw
:=
ar
.
Spec
.
Object
.
Raw
configmap
:=
v1
.
ConfigMap
{}
if
err
:=
json
.
Unmarshal
(
raw
,
&
configmap
);
err
!=
nil
{
configmap
:=
corev1
.
ConfigMap
{}
deserializer
:=
codecs
.
UniversalDeserializer
()
if
_
,
_
,
err
:=
deserializer
.
Decode
(
raw
,
nil
,
&
configmap
);
err
!=
nil
{
glog
.
Error
(
err
)
return
nil
return
toAdmissionReviewStatus
(
err
)
}
reviewStatus
:=
v1alpha1
.
AdmissionReviewStatus
{}
reviewStatus
.
Allowed
=
true
...
...
@@ -120,7 +120,34 @@ func admitConfigMaps(data []byte) *v1alpha1.AdmissionReviewStatus {
return
&
reviewStatus
}
type
admitFunc
func
(
data
[]
byte
)
*
v1alpha1
.
AdmissionReviewStatus
func
admitCRD
(
ar
v1alpha1
.
AdmissionReview
)
*
v1alpha1
.
AdmissionReviewStatus
{
glog
.
V
(
2
)
.
Info
(
"admitting crd"
)
cr
:=
struct
{
metav1
.
ObjectMeta
Data
map
[
string
]
string
}{}
raw
:=
ar
.
Spec
.
Object
.
Raw
err
:=
json
.
Unmarshal
(
raw
,
&
cr
)
if
err
!=
nil
{
glog
.
Error
(
err
)
return
toAdmissionReviewStatus
(
err
)
}
reviewStatus
:=
v1alpha1
.
AdmissionReviewStatus
{}
reviewStatus
.
Allowed
=
true
for
k
,
v
:=
range
cr
.
Data
{
if
k
==
"webhook-e2e-test"
&&
v
==
"webhook-disallow"
{
reviewStatus
.
Allowed
=
false
reviewStatus
.
Result
=
&
metav1
.
Status
{
Reason
:
"the custom resource contains unwanted data"
,
}
}
}
return
&
reviewStatus
}
type
admitFunc
func
(
v1alpha1
.
AdmissionReview
)
*
v1alpha1
.
AdmissionReviewStatus
func
serve
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
admit
admitFunc
)
{
var
body
[]
byte
...
...
@@ -137,9 +164,16 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {
return
}
reviewStatus
:=
admit
(
body
)
var
reviewStatus
*
v1alpha1
.
AdmissionReviewStatus
ar
:=
v1alpha1
.
AdmissionReview
{}
deserializer
:=
codecs
.
UniversalDeserializer
()
if
_
,
_
,
err
:=
deserializer
.
Decode
(
body
,
nil
,
&
ar
);
err
!=
nil
{
glog
.
Error
(
err
)
reviewStatus
=
toAdmissionReviewStatus
(
err
)
}
else
{
reviewStatus
=
admit
(
ar
)
}
if
reviewStatus
!=
nil
{
ar
.
Status
=
*
reviewStatus
}
...
...
@@ -156,10 +190,15 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {
func
servePods
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
serve
(
w
,
r
,
admitPods
)
}
func
serveConfigmaps
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
serve
(
w
,
r
,
admitConfigMaps
)
}
func
serveCRD
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
serve
(
w
,
r
,
admitCRD
)
}
func
main
()
{
var
config
Config
config
.
addFlags
()
...
...
@@ -167,6 +206,7 @@ func main() {
http
.
HandleFunc
(
"/pods"
,
servePods
)
http
.
HandleFunc
(
"/configmaps"
,
serveConfigmaps
)
http
.
HandleFunc
(
"/crd"
,
serveCRD
)
clientset
:=
getClient
()
server
:=
&
http
.
Server
{
Addr
:
":443"
,
...
...
test/images/webhook/scheme.go
0 → 100644
View file @
ab053a22
/*
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
main
import
(
admissionregistrationv1alpha1
"k8s.io/api/admissionregistration/v1alpha1"
corev1
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)
var
scheme
=
runtime
.
NewScheme
()
var
codecs
=
serializer
.
NewCodecFactory
(
scheme
)
func
init
()
{
addToScheme
(
scheme
)
}
func
addToScheme
(
scheme
*
runtime
.
Scheme
)
{
corev1
.
AddToScheme
(
scheme
)
admissionregistrationv1alpha1
.
AddToScheme
(
scheme
)
}
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