Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: pass a custom logger from user for the builder factory #571

Merged

Conversation

mojtaba-esk
Copy link
Member

@mojtaba-esk mojtaba-esk commented Oct 8, 2024

Closes #538

local tests are passing and the debug logs of the builder is shown when the debug level is set:

=== RUN   TestRunSuite
time="2024-10-08T16:53:15+03:30" level=info msg="Current log level" file="log/logger.go:41" env_log_level=LOG_LEVEL log_level=debug
time="2024-10-08T16:53:15+03:30" level=debug msg="set state of instance" file="instance/state.go:41" instance=timeout-handler state=Preparing
time="2024-10-08T16:53:15+03:30" level=debug msg="no need to build and push image for instance" file="instance/build.go:164" image="docker.io/bitnami/kubectl:latest" instance=timeout-handler
time="2024-10-08T16:53:15+03:30" level=debug msg="set state of instance" file="instance/state.go:41" instance=timeout-handler state=Committed
time="2024-10-08T16:53:15+03:30" level=debug msg="the namespace will be deleted" file="knuu/knuu.go:140" namespace=20241008-165248-541
time="2024-10-08T16:53:16+03:30" level=debug msg="started statefulSet" file="instance/execution.go:337" instance=timeout-handler
time="2024-10-08T16:53:16+03:30" level=debug msg="set state of instance" file="instance/state.go:41" instance=timeout-handler state=Started
time="2024-10-08T16:53:23+03:30" level=debug msg="Service created successfully." file="traefik/traefik.go:259" service=traefik
time="2024-10-08T16:53:36+03:30" level=debug msg="proxy endpoint" file="knuu/knuu.go:237" endpoint="151.115.12.124:80"
    suite_setup_test.go:53: Scope: 20241008-165248-541
=== RUN   TestRunSuite/TestBuildFromGit
=== PAUSE TestRunSuite/TestBuildFromGit
=== RUN   TestRunSuite/TestBuildFromGitWithModifications
=== PAUSE TestRunSuite/TestBuildFromGitWithModifications
=== CONT  TestRunSuite/TestBuildFromGit
=== CONT  TestRunSuite/TestBuildFromGitWithModifications
    build_image_test.go:22: Creating new instance
    build_image_test.go:26: Building the image
    build_image_test.go:64: Creating new instance
    build_image_test.go:68: Setting git repo
time="2024-10-08T16:53:36+03:30" level=debug msg="set state of instance" file="instance/state.go:41" instance=build-from-git state=Preparing
time="2024-10-08T16:53:36+03:30" level=debug msg="Building image ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h from git repo github.com/celestiaorg/knuu" file="container/docker.go:143"
time="2024-10-08T16:53:36+03:30" level=debug msg="set state of instance" file="instance/state.go:41" instance=build-from-git-with-modifications state=Preparing
time="2024-10-08T16:53:36+03:30" level=debug msg="Building image ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h from git repo github.com/celestiaorg/knuu" file="container/docker.go:143"
time=2024-10-08T16:53:54+03:30 level=debug msg=build logs: Enumerating objects: 2995, done.
Counting objects: 100% (1676/1676), done.
Compressing objects: 100% (904/904), done.
Total 2995 (delta 1265), reused 895 (delta 764), pack-reused 1319 (from 1)
INFO[0002] Retrieving image manifest ubuntu:18.04       
INFO[0002] Retrieving image ubuntu:18.04 from registry index.docker.io 
INFO[0003] Retrieving image manifest ubuntu:18.04       
INFO[0003] Returning cached image manifest              
INFO[0003] Built cross stage deps: map[]                
INFO[0003] Retrieving image manifest ubuntu:18.04       
INFO[0003] Returning cached image manifest              
INFO[0003] Retrieving image manifest ubuntu:18.04       
INFO[0003] Returning cached image manifest              
INFO[0003] Executing 0 build triggers                   
INFO[0003] Building stage 'ubuntu:18.04' [idx: '0', base-idx: '-1'] 
INFO[0003] Checking for cached layer ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h:d9ec0812facda4186859133fdf2d9fdff77bd1feaf4bf75effde8c60df58c240... 
INFO[0003] No cached layer found for cmd RUN echo "${MESSAGE}" > /test.txt 
INFO[0003] Unpacking rootfs as cmd RUN echo "${MESSAGE}" > /test.txt requires it. 
INFO[0004] ARG MESSAGE="Hello, World!"                  
INFO[0004] No files changed in this command, skipping snapshotting. 
INFO[0004] RUN echo "${MESSAGE}" > /test.txt            
INFO[0004] Initializing snapshotter ...                 
INFO[0004] Taking snapshot of full filesystem...        
INFO[0005] Cmd: /bin/sh                                 
INFO[0005] Args: [-c echo "${MESSAGE}" > /test.txt]     
INFO[0005] Running: [/bin/sh -c echo "${MESSAGE}" > /test.txt] 
INFO[0005] Taking snapshot of full filesystem...        
INFO[0005] Pushing layer ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h:d9ec0812facda4186859133fdf2d9fdff77bd1feaf4bf75effde8c60df58c240 to cache now 
INFO[0005] CMD ["sleep", "infinity"]                    
INFO[0005] No files changed in this command, skipping snapshotting. 
WARN[0005] Error uploading layer to cache: getting tag for destination: repository can only contain the characters `abcdefghijklmnopqrstuvwxyz0123456789_-./`: bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h 
INFO[0005] Pushing image to ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h 
INFO[0012] Pushed ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e@sha256:decb5331e1940ad72de02fa4b0816ec13cb7f98f9d0ca905e6ff41412a509fa6 
 file=container/docker.go:156
    build_image_test.go:36: Image built
time="2024-10-08T16:53:54+03:30" level=debug msg="no need to build and push image for instance" file="instance/build.go:164" image="ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h" instance=build-from-git
time="2024-10-08T16:53:54+03:30" level=debug msg="build logs: Enumerating objects: 2995, done.\nCounting objects:   0% (1/1641)\rCounting objects:   1% (17/1641)\rCounting objects:   2% (33/1641)\rCounting objects:   3% (50/1641)\rCounting objects:   4% (66/1641)\rCounting objects:   5% (83/1641)\rCounting objects:   6% (99/1641)\rCounting objects:   7% (115/1641)\rCounting objects:   8% (132/1641)\rCounting objects:   9% (148/1641)\rCounting objects:  10% (165/1641)\rCounting objects:  11% (181/1641)\rCounting objects:  12% (197/1641)\rCounting objects:  13% (214/1641)\rCounting objects:  14% (230/1641)\rCounting objects:  15% (247/1641)\rCounting objects:  16% (263/1641)\rCounting objects:  17% (279/1641)\rCounting objects:  18% (296/1641)\rCounting objects:  19% (312/1641)\rCounting objects:  20% (329/1641)\rCounting objects:  21% (345/1641)\rCounting objects:  22% (362/1641)\rCounting objects:  23% (378/1641)\rCounting objects:  24% (394/1641)\rCounting objects:  25% (411/1641)\rCounting objects:  26% (427/1641)\rCounting objects:  27% (444/1641)\rCounting objects:  28% (460/1641)\rCounting objects:  29% (476/1641)\rCounting objects:  30% (493/1641)\rCounting objects:  31% (509/1641)\rCounting objects:  32% (526/1641)\rCounting objects:  33% (542/1641)\rCounting objects:  34% (558/1641)\rCounting objects:  35% (575/1641)\rCounting objects:  36% (591/1641)\rCounting objects:  37% (608/1641)\rCounting objects:  38% (624/1641)\rCounting objects:  39% (640/1641)\rCounting objects:  40% (657/1641)\rCounting objects:  41% (673/1641)\rCounting objects:  42% (690/1641)\rCounting objects:  43% (706/1641)\rCounting objects:  44% (723/1641)\rCounting objects:  45% (739/1641)\rCounting objects:  46% (755/1641)\rCounting objects:  47% (772/1641)\rCounting objects:  48% (788/1641)\rCounting objects:  49% (805/1641)\rCounting objects:  50% (821/1641)\rCounting objects:  51% (837/1641)\rCounting objects:  52% (854/1641)\rCounting objects:  53% (870/1641)\rCounting objects:  54% (887/1641)\rCounting objects:  55% (903/1641)\rCounting objects:  56% (919/1641)\rCounting objects:  57% (936/1641)\rCounting objects:  58% (952/1641)\rCounting objects:  59% (969/1641)\rCounting objects:  60% (985/1641)\rCounting objects:  61% (1002/1641)\rCounting objects:  62% (1018/1641)\rCounting objects:  63% (1034/1641)\rCounting objects:  64% (1051/1641)\rCounting objects:  65% (1067/1641)\rCounting objects:  66% (1084/1641)\rCounting objects:  67% (1100/1641)\rCounting objects:  68% (1116/1641)\rCounting objects:  69% (1133/1641)\rCounting objects:  70% (1149/1641)\rCounting objects:  71% (1166/1641)\rCounting objects:  72% (1182/1641)\rCounting objects:  73% (1198/1641)\rCounting objects:  74% (1215/1641)\rCounting objects:  75% (1231/1641)\rCounting objects:  76% (1248/1641)\rCounting objects:  77% (1264/1641)\rCounting objects:  78% (1280/1641)\rCounting objects:  79% (1297/1641)\rCounting objects:  80% (1313/1641)\rCounting objects:  81% (1330/1641)\rCounting objects:  82% (1346/1641)\rCounting objects:  83% (1363/1641)\rCounting objects:  84% (1379/1641)\rCounting objects:  85% (1395/1641)\rCounting objects:  86% (1412/1641)\rCounting objects:  87% (1428/1641)\rCounting objects:  88% (1445/1641)\rCounting objects:  89% (1461/1641)\rCounting objects:  90% (1477/1641)\rCounting objects:  91% (1494/1641)\rCounting objects:  92% (1510/1641)\rCounting objects:  93% (1527/1641)\rCounting objects:  94% (1543/1641)\rCounting objects:  95% (1559/1641)\rCounting objects:  96% (1576/1641)\rCounting objects:  97% (1592/1641)\rCounting objects:  98% (1609/1641)\rCounting objects:  99% (1625/1641)\rCounting objects: 100% (1641/1641)\rCounting objects: 100% (1641/1641), done.\nCompressing objects:   0% (1/900)\rCompressing objects:   1% (9/900)\rCompressing objects:   2% (18/900)\rCompressing objects:   3% (27/900)\rCompressing objects:   4% (36/900)\rCompressing objects:   5% (45/900)\rCompressing objects:   6% (54/900)\rCompressing objects:   7% (63/900)\rCompressing objects:   8% (72/900)\rCompressing objects:   9% (81/900)\rCompressing objects:  10% (90/900)\rCompressing objects:  11% (99/900)\rCompressing objects:  12% (108/900)\rCompressing objects:  13% (117/900)\rCompressing objects:  14% (126/900)\rCompressing objects:  15% (135/900)\rCompressing objects:  16% (144/900)\rCompressing objects:  17% (153/900)\rCompressing objects:  18% (162/900)\rCompressing objects:  19% (171/900)\rCompressing objects:  20% (180/900)\rCompressing objects:  21% (189/900)\rCompressing objects:  22% (198/900)\rCompressing objects:  23% (207/900)\rCompressing objects:  24% (216/900)\rCompressing objects:  25% (225/900)\rCompressing objects:  26% (234/900)\rCompressing objects:  27% (243/900)\rCompressing objects:  28% (252/900)\rCompressing objects:  29% (261/900)\rCompressing objects:  30% (270/900)\rCompressing objects:  31% (279/900)\rCompressing objects:  32% (288/900)\rCompressing objects:  33% (297/900)\rCompressing objects:  34% (306/900)\rCompressing objects:  35% (315/900)\rCompressing objects:  36% (324/900)\rCompressing objects:  37% (333/900)\rCompressing objects:  38% (342/900)\rCompressing objects:  39% (351/900)\rCompressing objects:  40% (360/900)\rCompressing objects:  41% (369/900)\rCompressing objects:  42% (378/900)\rCompressing objects:  43% (387/900)\rCompressing objects:  44% (396/900)\rCompressing objects:  45% (405/900)\rCompressing objects:  46% (414/900)\rCompressing objects:  47% (423/900)\rCompressing objects:  48% (432/900)\rCompressing objects:  49% (441/900)\rCompressing objects:  50% (450/900)\rCompressing objects:  51% (459/900)\rCompressing objects:  52% (468/900)\rCompressing objects:  53% (477/900)\rCompressing objects:  54% (486/900)\rCompressing objects:  55% (495/900)\rCompressing objects:  56% (504/900)\rCompressing objects:  57% (513/900)\rCompressing objects:  58% (522/900)\rCompressing objects:  59% (531/900)\rCompressing objects:  60% (540/900)\rCompressing objects:  61% (549/900)\rCompressing objects:  62% (558/900)\rCompressing objects:  63% (567/900)\rCompressing objects:  64% (576/900)\rCompressing objects:  65% (585/900)\rCompressing objects:  66% (594/900)\rCompressing objects:  67% (603/900)\rCompressing objects:  68% (612/900)\rCompressing objects:  69% (621/900)\rCompressing objects:  70% (630/900)\rCompressing objects:  71% (639/900)\rCompressing objects:  72% (648/900)\rCompressing objects:  73% (657/900)\rCompressing objects:  74% (666/900)\rCompressing objects:  75% (675/900)\rCompressing objects:  76% (684/900)\rCompressing objects:  77% (693/900)\rCompressing objects:  78% (702/900)\rCompressing objects:  79% (711/900)\rCompressing objects:  80% (720/900)\rCompressing objects:  81% (729/900)\rCompressing objects:  82% (738/900)\rCompressing objects:  83% (747/900)\rCompressing objects:  84% (756/900)\rCompressing objects:  85% (765/900)\rCompressing objects:  86% (774/900)\rCompressing objects:  87% (783/900)\rCompressing objects:  88% (792/900)\rCompressing objects:  89% (801/900)\rCompressing objects:  90% (810/900)\rCompressing objects:  91% (819/900)\rCompressing objects:  92% (828/900)\rCompressing objects:  93% (837/900)\rCompressing objects:  94% (846/900)\rCompressing objects:  95% (855/900)\rCompressing objects:  96% (864/900)\rCompressing objects:  97% (873/900)\rCompressing objects:  98% (882/900)\rCompressing objects:  99% (891/900)\rCompressing objects: 100% (900/900)\rCompressing objects: 100% (900/900), done.\nTotal 2995 (delta 1229), reused 864 (delta 733), pack-reused 1354 (from 1)\n\x1b[36mINFO\x1b[0m[0002] Retrieving image manifest ubuntu:18.04       \n\x1b[36mINFO\x1b[0m[0002] Retrieving image ubuntu:18.04 from registry index.docker.io \n\x1b[36mINFO\x1b[0m[0003] Retrieving image manifest ubuntu:18.04       \n\x1b[36mINFO\x1b[0m[0003] Returning cached image manifest              \n\x1b[36mINFO\x1b[0m[0003] Built cross stage deps: map[]                \n\x1b[36mINFO\x1b[0m[0003] Retrieving image manifest ubuntu:18.04       \n\x1b[36mINFO\x1b[0m[0003] Returning cached image manifest              \n\x1b[36mINFO\x1b[0m[0003] Retrieving image manifest ubuntu:18.04       \n\x1b[36mINFO\x1b[0m[0003] Returning cached image manifest              \n\x1b[36mINFO\x1b[0m[0003] Executing 0 build triggers                   \n\x1b[36mINFO\x1b[0m[0003] Building stage 'ubuntu:18.04' [idx: '0', base-idx: '-1'] \n\x1b[36mINFO\x1b[0m[0003] Checking for cached layer ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h:d9ec0812facda4186859133fdf2d9fdff77bd1feaf4bf75effde8c60df58c240... \n\x1b[36mINFO\x1b[0m[0003] No cached layer found for cmd RUN echo \"${MESSAGE}\" > /test.txt \n\x1b[36mINFO\x1b[0m[0003] Unpacking rootfs as cmd RUN echo \"${MESSAGE}\" > /test.txt requires it. \n\x1b[36mINFO\x1b[0m[0004] ARG MESSAGE=\"Hello, World!\"                  \n\x1b[36mINFO\x1b[0m[0004] No files changed in this command, skipping snapshotting. \n\x1b[36mINFO\x1b[0m[0004] RUN echo \"${MESSAGE}\" > /test.txt            \n\x1b[36mINFO\x1b[0m[0004] Initializing snapshotter ...                 \n\x1b[36mINFO\x1b[0m[0004] Taking snapshot of full filesystem...        \n\x1b[36mINFO\x1b[0m[0005] Cmd: /bin/sh                                 \n\x1b[36mINFO\x1b[0m[0005] Args: [-c echo \"${MESSAGE}\" > /test.txt]     \n\x1b[36mINFO\x1b[0m[0005] Running: [/bin/sh -c echo \"${MESSAGE}\" > /test.txt] \n\x1b[36mINFO\x1b[0m[0005] Taking snapshot of full filesystem...        \n\x1b[36mINFO\x1b[0m[0005] CMD [\"sleep\", \"infinity\"]                    \n\x1b[36mINFO\x1b[0m[0005] Pushing layer ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h:d9ec0812facda4186859133fdf2d9fdff77bd1feaf4bf75effde8c60df58c240 to cache now \n\x1b[36mINFO\x1b[0m[0005] No files changed in this command, skipping snapshotting. \n\x1b[33mWARN\x1b[0m[0005] Error uploading layer to cache: getting tag for destination: repository can only contain the characters `abcdefghijklmnopqrstuvwxyz0123456789_-./`: bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h \n\x1b[36mINFO\x1b[0m[0005] Pushing image to ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h \n\x1b[36mINFO\x1b[0m[0013] Pushed ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e@sha256:47b88f416c7038d8a7b39df32fa8d37f2fe94a4254fc28a2c6f6f980a76cee2d \n" file="container/docker.go:156"
time=2024-10-08T16:53:54+03:30 level=debug msg=set state of instance file=instance/state.go:41 instance=build-from-git state=Committed
    build_image_test.go:40: Starting instance
time=2024-10-08T16:53:54+03:30 level=debug msg=Generated image hash: 84a72f66837481462173d732f39d6b7705af88ead95b52d12b9dc8be93af9d01 file=container/docker.go:194
time=2024-10-08T16:53:54+03:30 level=debug msg=cannot use any cached image for instance file=instance/build.go:197 instance=build-from-git-with-modifications
time=2024-10-08T16:53:55+03:30 level=debug msg=started statefulSet file=instance/execution.go:337 instance=build-from-git
time=2024-10-08T16:53:55+03:30 level=debug msg=set state of instance file=instance/state.go:41 instance=build-from-git state=Started
    build_image_test.go:43: Instance started
    build_image_test.go:45: Getting file bytes
time=2024-10-08T16:54:14+03:30 level=debug msg=build logs: INFO[0001] Retrieving image manifest ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h 
INFO[0001] Retrieving image ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h from registry ttl.sh 
INFO[0002] Built cross stage deps: map[]                
INFO[0002] Retrieving image manifest ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h 
INFO[0002] Returning cached image manifest              
INFO[0002] Executing 0 build triggers                   
INFO[0002] Building stage 'ttl.sh/bb7a5c161d9535a225a7fa67e665b4d8305204a82bff3c75cb925cf578f20b7e:24h' [idx: '0', base-idx: '-1'] 
INFO[0002] Unpacking rootfs as cmd ADD --chown=root:root /home/hello.txt /home/hello.txt requires it. 
INFO[0004] Using files from context: [/kaniko/buildcontext/home/hello.txt] 
INFO[0004] ADD --chown=root:root /home/hello.txt /home/hello.txt 
INFO[0004] Taking snapshot of files...                  
INFO[0004] Pushing image to ttl.sh/84a72f66837481462173d732f39d6b7705af88ead95b52d12b9dc8be93af9d01:24h 
INFO[0009] Pushed ttl.sh/84a72f66837481462173d732f39d6b7705af88ead95b52d12b9dc8be93af9d01@sha256:95f7db225468fcce2b3753622b2752b7ad0228a2053dcb7392ba9a6e41c7fa13 
 file=container/docker.go:121
time=2024-10-08T16:54:14+03:30 level=debug msg=pushed new image for instance file=instance/build.go:208 image=ttl.sh/84a72f66837481462173d732f39d6b7705af88ead95b52d12b9dc8be93af9d01:24h instance=build-from-git-with-modifications
time=2024-10-08T16:54:14+03:30 level=debug msg=set state of instance file=instance/state.go:41 instance=build-from-git-with-modifications state=Committed
time=2024-10-08T16:54:15+03:30 level=debug msg=started statefulSet file=instance/execution.go:337 instance=build-from-git-with-modifications
time=2024-10-08T16:54:15+03:30 level=debug msg=set state of instance file=instance/state.go:41 instance=build-from-git-with-modifications state=Started
=== NAME  TestRunSuite
    suite.go:59: Cleaning up knuu...
    suite.go:63: Knuu is cleaned up
--- PASS: TestRunSuite (47.82s)
    --- PASS: TestRunSuite/TestBuildFromGit (26.41s)
    --- PASS: TestRunSuite/TestBuildFromGitWithModifications (46.72s)
PASS
ok  	github.com/celestiaorg/knuu/e2e/system	94.574s

Summary by CodeRabbit

  • New Features

    • Enhanced logging capabilities in the BuilderFactory structure.
    • Introduced a new structured format for BuilderFactory options.
  • Bug Fixes

    • Added specific error messages for empty fields related to image and build context management.
  • Refactor

    • Updated methods to utilize the new BuilderFactoryOptions struct for improved parameter handling.
    • Refactored the instantiation of the BuilderFactory in the SetImage and SetGitRepo methods.

@mojtaba-esk mojtaba-esk requested a review from a team October 8, 2024 13:27
@mojtaba-esk mojtaba-esk self-assigned this Oct 8, 2024
Copy link
Contributor

coderabbitai bot commented Oct 8, 2024

Walkthrough

The changes involve significant modifications to the BuilderFactory structure in the pkg/container/docker.go file, including the introduction of a new logger field and a refactored constructor that accepts a BuilderFactoryOptions struct. Additionally, four new error variables have been added in pkg/container/errors.go to enhance error reporting. The SetImage and SetGitRepo methods in pkg/instance/build.go have been updated to utilize the new BuilderFactoryOptions struct for instantiating the BuilderFactory.

Changes

File Change Summary
pkg/container/docker.go - Added BuilderFactoryOptions struct with fields: ImageName, BuildContext, ImageBuilder, Args, Logger.
- Refactored NewBuilderFactory to accept BuilderFactoryOptions.
- Added logger *logrus.Logger field to BuilderFactory.
- Updated logging in PushBuilderImage, BuildImageFromGitRepo, GenerateImageHash methods.
pkg/container/errors.go - Introduced four new error variables: ErrImageNameEmpty, ErrBuildContextEmpty, ErrImageBuilderNotSet, ErrLoggerEmpty.
pkg/instance/build.go - Updated SetImage and SetGitRepo methods to instantiate BuilderFactory using BuilderFactoryOptions.

Poem

In the land of code where rabbits hop,
A logger's added, and errors won't stop.
With options bundled, neat and tight,
The builders now work with all their might.
Hooray for changes, let’s give a cheer,
For better logging, the future is near! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
pkg/instance/build.go (2)

57-63: LGTM! Consider adding a comment for clarity.

The refactoring to use BuilderFactoryOptions improves code organization and aligns with the PR objective of passing a custom logger. This change enhances flexibility and maintainability.

Consider adding a brief comment explaining the purpose of the BuilderFactoryOptions struct for better code documentation:

+// Use BuilderFactoryOptions to configure the builder factory with custom settings
 factory, err := container.NewBuilderFactory(container.BuilderFactoryOptions{
   ImageName:    image,
   BuildContext: b.getBuildDir(),
   ImageBuilder: b.instance.ImageBuilder,
   Args:         args,
   Logger:       b.instance.Logger,
 })

89-95: LGTM! Consider refactoring to reduce duplication.

The changes in this method are consistent with those in SetImage, improving code organization and aligning with the PR objective. This consistency is commendable.

To further improve the code, consider extracting the common BuilderFactory creation logic into a separate method to reduce duplication:

func (b *build) createBuilderFactory(imageName string, args []builder.ArgInterface) (*container.BuilderFactory, error) {
	return container.NewBuilderFactory(container.BuilderFactoryOptions{
		ImageName:    imageName,
		BuildContext: b.getBuildDir(),
		ImageBuilder: b.instance.ImageBuilder,
		Args:         args,
		Logger:       b.instance.Logger,
	})
}

Then, you can use this method in both SetImage and SetGitRepo:

factory, err := b.createBuilderFactory(image, args)

This refactoring would centralize the factory creation logic, making future modifications easier and reducing the risk of inconsistencies between the two methods.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 339f749 and 904f9f7.

📒 Files selected for processing (3)
  • pkg/container/docker.go (6 hunks)
  • pkg/container/errors.go (1 hunks)
  • pkg/instance/build.go (2 hunks)
🧰 Additional context used
🔇 Additional comments (7)
pkg/container/errors.go (5)

28-28: LGTM: New error variable for empty image name

The addition of ErrImageNameEmpty is appropriate and follows the existing error definition pattern. It will help in providing more specific error messages when validating inputs for the builder factory.


29-29: LGTM: New error variable for empty build context

The addition of ErrBuildContextEmpty is appropriate and consistent with the existing error definition pattern. It will enhance error reporting when validating the build context for the builder factory.


30-30: LGTM: New error variable for empty image builder

The addition of ErrImageBuilderEmpty is appropriate and follows the existing error definition pattern. It will provide more specific error messages when validating the image builder input for the builder factory.


31-31: LGTM: New error variable for empty logger

The addition of ErrLoggerEmpty is appropriate and consistent with the existing error definition pattern. This error specifically addresses the PR objective of allowing users to pass a custom logger to the builder factory, enhancing the validation process for this new feature.


28-31: Overall assessment: Excellent additions to error handling

The four new error variables (ErrImageNameEmpty, ErrBuildContextEmpty, ErrImageBuilderEmpty, and ErrLoggerEmpty) are well-defined and consistent with the existing error handling pattern. These additions significantly improve input validation for the builder factory, aligning perfectly with the PR objectives. The changes enhance the robustness of the code without modifying existing functionality, which is a best practice in software development.

pkg/instance/build.go (1)

Line range hint 1-368: Overall, the changes look good and achieve the PR objectives.

The modifications to SetImage and SetGitRepo methods successfully implement the ability to pass a custom logger to the builder factory. The use of BuilderFactoryOptions improves code organization and flexibility. The changes are consistent across both methods, indicating a thoughtful approach to the refactoring.

A few minor suggestions have been provided to further enhance code quality:

  1. Adding a brief comment to explain the purpose of BuilderFactoryOptions.
  2. Considering a refactoring to extract common BuilderFactory creation logic to reduce duplication.

These suggestions are optional and do not impact the functionality or correctness of the implemented changes.

pkg/container/docker.go (1)

153-157: Same concerns as in lines 118-122 regarding logger's Formatter

The pattern here repeats the issues noted previously:

  • Potential panic due to unchecked type assertion.
  • Modifying the shared logger's formatter directly, which may cause unintended side effects.

Please address these concerns as suggested in the earlier comments.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
pkg/container/docker.go (3)

28-33: LGTM: BuilderFactoryOptions struct introduced

The new BuilderFactoryOptions struct effectively encapsulates all parameters for creating a BuilderFactory. This change improves code organization and makes it easier to extend the factory creation process in the future. The inclusion of the Logger field aligns with the PR objective of allowing custom loggers.

Consider adding comments to each field in the struct to improve documentation:

type BuilderFactoryOptions struct {
    ImageName    string              // Name of the base image
    BuildContext string              // Path to the build context directory
    ImageBuilder builder.Builder     // Image builder implementation
    Args         []builder.ArgInterface // Build arguments
    Logger       *logrus.Logger      // Custom logger instance
}

37-52: LGTM: NewBuilderFactory refactored to use BuilderFactoryOptions

The refactoring of NewBuilderFactory to use BuilderFactoryOptions simplifies the function signature and improves extensibility. The addition of options verification enhances error handling, which is a good practice.

Consider adding a comment to explain the purpose of the verifyOptions function:

// verifyOptions checks if the provided BuilderFactoryOptions are valid
if err := verifyOptions(opts); err != nil {
    return nil, err
}

The initialization of BuilderFactory using the options struct is correct and includes the new logger field, which aligns with the PR objective.


207-218: LGTM: verifyOptions function added

The addition of the verifyOptions function improves error handling by validating the BuilderFactoryOptions. The checks are comprehensive and return appropriate errors.

Consider adding a check for ImageBuilder as well, since it's used in other methods and has nil checks:

if opts.ImageBuilder == nil {
    return ErrImageBuilderNotSet
}

This would provide more complete validation of the options.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 904f9f7 and f81d2cc.

📒 Files selected for processing (2)
  • pkg/container/docker.go (6 hunks)
  • pkg/container/errors.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/container/errors.go
🧰 Additional context used
🔇 Additional comments (7)
pkg/container/docker.go (7)

25-26: LGTM: Logger field added to BuilderFactory

The addition of the logger field to the BuilderFactory struct aligns with the PR objective of enabling custom logging. This change enhances the factory's logging capabilities and allows for more flexible logging configurations.


90-90: LGTM: Debug log added

The addition of this debug log improves visibility into the method's behavior when no changes are made to the image.


111-113: LGTM: Nil check for imageBuilder added

The addition of this nil check prevents potential nil pointer dereferences and improves error handling.


147-148: LGTM: Debug log added

The addition of this debug log improves visibility into the method's behavior when building an image from a git repository.


149-151: LGTM: Nil check for imageBuilder added

The addition of this nil check prevents potential nil pointer dereferences and improves error handling.


202-202: LGTM: Debug log added for image hash

The addition of this debug log improves visibility by logging the generated image hash. This can be helpful for debugging and tracing purposes.


Line range hint 1-218: Overall assessment: LGTM with minor suggestions

The changes in this file successfully implement the feature of passing a custom logger to the builder factory, aligning with the PR objective. The introduction of BuilderFactoryOptions and the refactoring of NewBuilderFactory improve code organization and extensibility. The addition of debug logs and nil checks enhances error handling and visibility.

Some minor improvements have been suggested:

  1. Adding comments to the BuilderFactoryOptions struct fields.
  2. Improving the handling of logger formatter modifications in PushBuilderImage and BuildImageFromGitRepo methods.
  3. Adding a check for ImageBuilder in the verifyOptions function.

These suggestions, if implemented, would further enhance the code quality and robustness.

pkg/container/docker.go Outdated Show resolved Hide resolved
pkg/container/docker.go Outdated Show resolved Hide resolved
Copy link
Member

@MSevey MSevey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions around the validation method.

Copy link
Contributor

@tty47 tty47 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@MSevey MSevey enabled auto-merge (squash) October 11, 2024 20:18
@mojtaba-esk mojtaba-esk merged commit dc5ae81 into main Oct 14, 2024
11 of 12 checks passed
@mojtaba-esk mojtaba-esk deleted the mojtaba/538-use-a-custom-logger-from-user-for-the-builder-2 branch October 14, 2024 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use a custom logger from user for the builder
3 participants