- .devcontainer
- .github
- .nvim
- .vscode
-
Documentation
-
_api
-
_exts
-
_static
-
_templates
-
bpf
-
cmdref
-
community
-
configuration
-
contributing
-
gettingstarted
-
images
-
installation
-
internals
-
network
-
observability
-
operations
-
overview
-
requirements-min
-
robots
-
security
- .dockerignore
- .gitignore
- .readthedocs.yaml
- Dockerfile
- Makefile
- api.rst
- beta.rst
- cheatsheet.rst
- check-build.sh
- check-cmdref.sh
- check-codeowners.sh
- check-crd-compat-table.sh
- check-crdlist.sh
- check-examples.sh
- check-flaggen.sh
- check-helmvalues.sh
- check-links.sh
- codeowners.rst
- conf.py
- crdlist.rst
- further_reading.rst
- glossary.rst
- grpcapi.rst
- helm-reference.rst
- helm-values.rst
- helm-values.tmp.tmpl
- index.rst
- kvstore.rst
- requirements.txt
- runtime.txt
- spelling_wordlist.txt
- update-cmdref.sh
- update-codeowners.sh
- update-docs-builder-image.sh
- update-spelling_wordlist.sh
- yaml.config
-
_api
- api
- bpf
- bugtool
- cilium-dbg
- cilium-health
- clustermesh-apiserver
- contrib
- daemon
-
examples
-
crds
-
hubble
-
kubernetes
-
kubernetes-cassandra
-
kubernetes-dns
-
kubernetes-egress-gateway
-
kubernetes-es
-
kubernetes-external-ips
-
kubernetes-grpc
-
kubernetes-ip-masq-agent
-
kubernetes-istio
-
kubernetes-kafka
-
kubernetes-local-redirect
-
kubernetes-memcached
-
kubernetes-tls-inspection
-
minikube
-
misc
-
policies
- valid-labels.lpc
-
crds
- hack
- hubble-relay
- images
- install
- operator
-
pkg
-
alibabacloud
-
alignchecker
-
allocator
-
annotation
-
api
-
auth
-
aws
-
azure
-
backoff
-
bgp
-
bgpv1
-
bpf
-
byteorder
-
cgroups
-
checker
-
cidr
-
ciliumenvoyconfig
-
cleanup
-
client
-
clustermesh
-
command
-
common
-
comparator
-
completion
-
components
-
container
-
controller
-
counter
-
crypto
-
datapath
-
debug
-
defaults
-
ebpf
-
egressgateway
-
elf
-
endpoint
-
endpointcleanup
-
endpointmanager
-
endpointstate
-
envoy
-
eventqueue
-
flowdebug
-
fqdn
-
fswatcher
-
gops
-
health
-
healthv2
-
hive
-
hubble
-
iana
-
identity
-
idpool
-
inctimer
-
ip
-
ipalloc
-
ipam
-
ipcache
-
ipmasq
-
k8s
-
kvstore
-
l2announcer
-
labels
-
labelsfilter
-
launcher
-
loadbalancer
-
loadinfo
-
lock
-
logging
-
mac
-
maglev
-
maps
-
math
-
mcastmanager
-
metrics
-
monitor
-
mountinfo
-
mtu
-
multicast
-
murmur3
-
netns
-
node
-
nodediscovery
-
option
-
pidfile
-
policy
-
pprof
-
promise
-
proxy
-
rand
-
rate
-
recorder
-
redirectpolicy
-
resiliency
-
revert
-
safeio
-
safetime
-
service
-
signal
-
slices
-
socketlb
-
source
-
spanstat
-
statedb
-
status
-
testutils
-
time
-
trigger
-
tuple
-
types
-
u8proto
-
version
-
versioncheck
-
wireguard
-
alibabacloud
- plugins
-
test
-
bigtcp
-
bpf
-
config
-
consul
-
controlplane
-
eks
-
envoy
-
fuzzing
-
ginkgo-ext
-
gke
-
helpers
-
k8s
-
l4lb
-
logger
-
nat46x64
-
packet
-
provision
-
runtime
-
standalone
-
verifier
-
vtep
- Makefile
- README.md
- Vagrantfile
- archive_test_results.sh
- archive_test_results_eks.sh
- get-gh-comment-info.py
- get-vagrant-kubeconfig.sh
- kubernetes-test.sh
- post_build_agent.sh
- print-node-ip.sh
- test_suite_test.go
- vagrant-ci-start.sh
- vagrant-local-create-box.sh
- vagrant-local-start-runtime.sh
- vagrant-local-start.sh
- vagrant_cleanup.sh
-
bigtcp
- tools
- vendor
- .authors.aux
- .clang-format
- .clomonitor.yml
- .gitattributes
- .gitignore
- .golangci.yaml
- .mailmap
- .openvex.json
- AUTHORS
- CODEOWNERS
- CODE_OF_CONDUCT.md
- CONTRIBUTING.md
- FURTHER_READINGS.rst
- LICENSE
- MAINTAINERS.md
- Makefile
- Makefile.defs
- Makefile.docker
- Makefile.kind
- Makefile.quiet
- README.rst
- SECURITY-INSIGHTS.yml
- SECURITY.md
- USERS.md
- VERSION
- Vagrantfile
- go.mod
- go.sum
- netlify.toml
- stable.txt
- vagrant_box_defaults.rb
Explanation
This code demonstrates how to use the resource
package from Cilium’s Kubernetes integration to observe and process changes in Pods and Services. It shows how to use the Store
and Events
functions to retrieve data and subscribe to events, respectively.
Here’s a breakdown of the code:
Hive:
The code leverages the
hive
package for dependency injection and modularity.client.Cell
: This provides the Kubernetes clientset for accessing the Kubernetes API.resourcesCell
: A cell that provides instances ofresource.Resource
for both Pods and Services.printServicesCell
: A cell that creates an instance of thePrintServices
type.resourcesCell
:This cell uses the Kubernetes client to create
resource.Resource
instances for Pods and Services.It uses a
utils.ListerWatcher
to watch for changes to Pods and Services in the Kubernetes API.The
resource.New
function initializes theresource.Resource
instances with the lister-watcher and an optional metric name (“Pod” or “Service”).printServicesCell
:It defines the
PrintServices
struct, which is responsible for observing and processing changes in Pods and Services.The
newPrintServices
function creates an instance ofPrintServices
and provides the necessary dependencies.PrintServices
struct:It contains a worker pool to process the event loop and holds references to the
resource.Resource
instances for Pods and Services.Start
function:Initializes the worker pool and starts the
processLoop
.It does a blocking dump of all services using the
Store
function to demonstrate initial data retrieval.Stop
function:Closes the worker pool to stop the event loop.
printServices
function:Retrieves a handle to the store of Services and prints the services and their selectors.
processLoop
function:Contains the event loop logic.
Subscribes to events from both Pods and Services using the
Events
function.Keeps track of Pod labels and Service selectors in maps.
Periodically prints the services and the Pods that they select.
Handles events from the
Events
channel:Sync
: Indicates the initial sync has completed.Upsert
: Represents an update or creation of a Pod or Service.Delete
: Represents a deletion of a Pod or Service.Includes a simulated fault to demonstrate error handling.
Key Concepts:
resource.Resource
: A type that provides a convenient way to interact with Kubernetes resources. It allows retrieving a store (for retrieving all objects) and subscribing to events.Store
: Provides access to a snapshot of the Kubernetes resources.Events
: Returns a channel that emits events when Kubernetes resources change.- Event Kinds:
Sync
,Upsert
, andDelete
represent the different types of events that can occur. - Worker Pool: Used for managing the event loop to process events efficiently.
Overall, this code provides a basic example of how to use the resource
package to observe Kubernetes resources and process their changes.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph