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
fa98310a
Commit
fa98310a
authored
Jul 27, 2017
by
duan-yue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor capabilities to a singleton struct
parent
d8205661
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
13 deletions
+72
-13
BUILD
pkg/capabilities/BUILD
+8
-0
capabilities.go
pkg/capabilities/capabilities.go
+14
-13
capabilities_test.go
pkg/capabilities/capabilities_test.go
+50
-0
No files found.
pkg/capabilities/BUILD
View file @
fa98310a
...
@@ -5,6 +5,7 @@ licenses(["notice"])
...
@@ -5,6 +5,7 @@ licenses(["notice"])
load(
load(
"@io_bazel_rules_go//go:def.bzl",
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_library",
"go_test",
)
)
go_library(
go_library(
...
@@ -16,6 +17,13 @@ go_library(
...
@@ -16,6 +17,13 @@ go_library(
tags = ["automanaged"],
tags = ["automanaged"],
)
)
go_test(
name = "go_default_test",
srcs = ["capabilities_test.go"],
library = ":go_default_library",
tags = ["automanaged"],
)
filegroup(
filegroup(
name = "package-srcs",
name = "package-srcs",
srcs = glob(["**"]),
srcs = glob(["**"]),
...
...
pkg/capabilities/capabilities.go
View file @
fa98310a
...
@@ -46,16 +46,17 @@ type PrivilegedSources struct {
...
@@ -46,16 +46,17 @@ type PrivilegedSources struct {
HostIPCSources
[]
string
HostIPCSources
[]
string
}
}
// TODO: Clean these up into a singleton
var
capInstance
struct
{
var
once
sync
.
Once
once
sync
.
Once
var
lock
sync
.
Mutex
lock
sync
.
Mutex
var
capabilities
*
Capabilities
capabilities
*
Capabilities
}
// Initialize the capability set. This can only be done once per binary, subsequent calls are ignored.
// Initialize the capability set. This can only be done once per binary, subsequent calls are ignored.
func
Initialize
(
c
Capabilities
)
{
func
Initialize
(
c
Capabilities
)
{
// Only do this once
// Only do this once
once
.
Do
(
func
()
{
capInstance
.
once
.
Do
(
func
()
{
capabilities
=
&
c
cap
Instance
.
cap
abilities
=
&
c
})
})
}
}
...
@@ -70,17 +71,17 @@ func Setup(allowPrivileged bool, privilegedSources PrivilegedSources, perConnect
...
@@ -70,17 +71,17 @@ func Setup(allowPrivileged bool, privilegedSources PrivilegedSources, perConnect
// SetForTests sets capabilities for tests. Convenience method for testing. This should only be called from tests.
// SetForTests sets capabilities for tests. Convenience method for testing. This should only be called from tests.
func
SetForTests
(
c
Capabilities
)
{
func
SetForTests
(
c
Capabilities
)
{
lock
.
Lock
()
capInstance
.
lock
.
Lock
()
defer
lock
.
Unlock
()
defer
capInstance
.
lock
.
Unlock
()
capabilities
=
&
c
cap
Instance
.
cap
abilities
=
&
c
}
}
// Returns a read-only copy of the system capabilities.
// Returns a read-only copy of the system capabilities.
func
Get
()
Capabilities
{
func
Get
()
Capabilities
{
lock
.
Lock
()
capInstance
.
lock
.
Lock
()
defer
lock
.
Unlock
()
defer
capInstance
.
lock
.
Unlock
()
// This check prevents clobbering of capabilities that might've been set via SetForTests
// This check prevents clobbering of capabilities that might've been set via SetForTests
if
capabilities
==
nil
{
if
cap
Instance
.
cap
abilities
==
nil
{
Initialize
(
Capabilities
{
Initialize
(
Capabilities
{
AllowPrivileged
:
false
,
AllowPrivileged
:
false
,
PrivilegedSources
:
PrivilegedSources
{
PrivilegedSources
:
PrivilegedSources
{
...
@@ -90,5 +91,5 @@ func Get() Capabilities {
...
@@ -90,5 +91,5 @@ func Get() Capabilities {
},
},
})
})
}
}
return
*
capabilities
return
*
cap
Instance
.
cap
abilities
}
}
pkg/capabilities/capabilities_test.go
0 → 100644
View file @
fa98310a
/*
Copyright 2014 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
capabilities
import
(
"reflect"
"testing"
)
func
TestGet
(
t
*
testing
.
T
)
{
defaultCap
:=
Capabilities
{
AllowPrivileged
:
false
,
PrivilegedSources
:
PrivilegedSources
{
HostNetworkSources
:
[]
string
{},
HostPIDSources
:
[]
string
{},
HostIPCSources
:
[]
string
{},
},
}
res
:=
Get
()
if
!
reflect
.
DeepEqual
(
defaultCap
,
res
)
{
t
.
Fatalf
(
"expected Capabilities: %#v, got a non-default: %#v"
,
defaultCap
,
res
)
}
cap
:=
Capabilities
{
PrivilegedSources
:
PrivilegedSources
{
HostNetworkSources
:
[]
string
{
"A"
,
"B"
},
},
}
SetForTests
(
cap
)
res
=
Get
()
if
!
reflect
.
DeepEqual
(
cap
,
res
)
{
t
.
Fatalf
(
"expected Capabilities: %#v , got a different: %#v"
,
cap
,
res
)
}
}
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